diff --git a/DesignPattern/AbstractFactoryPattern/Factory.cpp b/DesignPattern/AbstractFactoryPattern/Factory.cpp new file mode 100644 index 0000000..4ad2e7c --- /dev/null +++ b/DesignPattern/AbstractFactoryPattern/Factory.cpp @@ -0,0 +1,25 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#include "Factory.h" +#include "concrete_factory.h" + +Factory* Factory::CreateFactory(FACTORY_TYPE factory) +{ + Factory *pFactory = nullptr; + switch (factory) { + case FACTORY_TYPE::BENZ_FACTORY: // 奔驰工厂 + pFactory = new BenzFactory(); + break; + case FACTORY_TYPE::BMW_FACTORY: // 宝马工厂 + pFactory = new BmwFactory(); + break; + case FACTORY_TYPE::AUDI_FACTORY: // 奥迪工厂 + pFactory = new AudiFactory(); + break; + default: + break; + } + return pFactory; +} \ No newline at end of file diff --git a/DesignPattern/AbstractFactoryPattern/Factory.h b/DesignPattern/AbstractFactoryPattern/Factory.h new file mode 100644 index 0000000..b00ed46 --- /dev/null +++ b/DesignPattern/AbstractFactoryPattern/Factory.h @@ -0,0 +1,24 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#ifndef DESIGNPATTERN_FACTORY_H +#define DESIGNPATTERN_FACTORY_H + +#include "product.h" + +// 抽象工厂模式 +class Factory { +public: + enum FACTORY_TYPE { + BENZ_FACTORY, // 奔驰工厂 + BMW_FACTORY, // 宝马工厂 + AUDI_FACTORY // 奥迪工厂 + }; + + virtual ICar* CreateCar() = 0; // 生产汽车 + virtual IBike* CreateBike() = 0; // 生产自行车 + static Factory * CreateFactory(FACTORY_TYPE factory); // 创建工厂 +}; + +#endif //DESIGNPATTERN_FACTORY_H diff --git a/DesignPattern/AbstractFactoryPattern/FactoryMain.cpp b/DesignPattern/AbstractFactoryPattern/FactoryMain.cpp new file mode 100644 index 0000000..b819e2d --- /dev/null +++ b/DesignPattern/AbstractFactoryPattern/FactoryMain.cpp @@ -0,0 +1,46 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#include "Factory.h" +#include "product.h" +#include "FactoryMain.h" +#include +using namespace std; + +void FactoryMain() +{ + // + Factory * pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::BENZ_FACTORY); + ICar * pCar = pFactory->CreateCar(); + IBike * pBike = pFactory->CreateBike(); + + cout << "Benz factory - Car: " << pCar->Name() << endl; + cout << "Benz factory - Bike: " << pBike->Name() << endl; + + SAFE_DELETE(pCar); + SAFE_DELETE(pBike); + SAFE_DELETE(pFactory); + + // + pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::BMW_FACTORY); + pCar = pFactory->CreateCar(); + pBike = pFactory->CreateBike(); + cout << "Bmw factory - Car: " << pCar->Name() << endl; + cout << "Bmw factory - Bike: " << pBike->Name() << endl; + + SAFE_DELETE(pCar); + SAFE_DELETE(pBike); + SAFE_DELETE(pFactory); + + // µ + pFactory = Factory::CreateFactory(Factory::FACTORY_TYPE::AUDI_FACTORY); + pCar = pFactory->CreateCar(); + pBike = pFactory->CreateBike(); + cout << "Audi factory - Car: " << pCar->Name() << endl; + cout << "Audi factory - Bike: " << pBike->Name() << endl; + + SAFE_DELETE(pCar); + SAFE_DELETE(pBike); + SAFE_DELETE(pFactory); +} \ No newline at end of file diff --git a/DesignPattern/AbstractFactoryPattern/FactoryMain.h b/DesignPattern/AbstractFactoryPattern/FactoryMain.h new file mode 100644 index 0000000..e1cfe21 --- /dev/null +++ b/DesignPattern/AbstractFactoryPattern/FactoryMain.h @@ -0,0 +1,14 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#ifndef DESIGNPATTERN_FACTORYMAIN_H +#define DESIGNPATTERN_FACTORYMAIN_H + +#ifndef SAFE_DELETE +#define SAFE_DELETE(p) { if(p) {delete(p); (p)=nullptr;}} +#endif + +void FactoryMain(); + +#endif //DESIGNPATTERN_FACTORYMAIN_H diff --git a/DesignPattern/AbstractFactoryPattern/concrete_factory.h b/DesignPattern/AbstractFactoryPattern/concrete_factory.h new file mode 100644 index 0000000..8de0baf --- /dev/null +++ b/DesignPattern/AbstractFactoryPattern/concrete_factory.h @@ -0,0 +1,51 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#ifndef DESIGNPATTERN_CONCRETE_FACTORY_H +#define DESIGNPATTERN_CONCRETE_FACTORY_H + +#include "Factory.h" +#include "concrete_product.h" + +// 奔驰工厂 +class BenzFactory : public Factory +{ +public: + ICar* CreateCar() + { + return new BenzCar(); + } + IBike* CreateBike() + { + return new BenzBike(); + } +}; + +// 宝马工厂 +class BmwFactory : public Factory +{ +public: + ICar* CreateCar() { + return new BmwCar(); + } + + IBike* CreateBike() { + return new BmwBike(); + } +}; + +// 奥迪工厂 +class AudiFactory : public Factory +{ +public: + ICar* CreateCar() { + return new AudiCar(); + } + + IBike* CreateBike() { + return new AudiBike(); + } +}; + +#endif //DESIGNPATTERN_CONCRETE_FACTORY_H diff --git a/DesignPattern/AbstractFactoryPattern/concrete_product.h b/DesignPattern/AbstractFactoryPattern/concrete_product.h new file mode 100644 index 0000000..fef09bb --- /dev/null +++ b/DesignPattern/AbstractFactoryPattern/concrete_product.h @@ -0,0 +1,72 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#ifndef DESIGNPATTERN_CONCRETE_PRODUCT_H +#define DESIGNPATTERN_CONCRETE_PRODUCT_H + +#include "product.h" + +/********** 汽车 **********/ +// 奔驰 +class BenzCar : public ICar +{ +public: + string Name() + { + return "Benz Car"; + } +}; + +// 宝马 +class BmwCar : public ICar +{ +public: + string Name() + { + return "Bmw Car"; + } +}; + +// 奥迪 +class AudiCar : public ICar +{ +public: + string Name() + { + return "Audi Car"; + } +}; + +/********** 自行车 **********/ +// 奔驰 +class BenzBike : public IBike +{ +public: + string Name() + { + return "Benz Bike"; + } +}; + +// 宝马 +class BmwBike : public IBike +{ +public: + string Name() + { + return "Bmw Bike"; + } +}; + +// 奥迪 +class AudiBike : public IBike +{ +public: + string Name() + { + return "Audi Bike"; + } +}; + +#endif //DESIGNPATTERN_CONCRETE_PRODUCT_H diff --git a/DesignPattern/AbstractFactoryPattern/product.h b/DesignPattern/AbstractFactoryPattern/product.h new file mode 100644 index 0000000..4d62240 --- /dev/null +++ b/DesignPattern/AbstractFactoryPattern/product.h @@ -0,0 +1,25 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#ifndef DESIGNPATTERN_PRODUCT_H +#define DESIGNPATTERN_PRODUCT_H + +#include +using std::string; + +// 汽车接口 +class ICar +{ +public: + virtual string Name() = 0; +}; + +// 自行车接口 +class IBike +{ +public: + virtual string Name() = 0; +}; + +#endif //DESIGNPATTERN_PRODUCT_H diff --git a/DesignPattern/AdapterPattern/AdapterMain.h b/DesignPattern/AdapterPattern/AdapterMain.h new file mode 100644 index 0000000..8e58cba --- /dev/null +++ b/DesignPattern/AdapterPattern/AdapterMain.h @@ -0,0 +1,21 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#ifndef DESIGNPATTERN_ADAPTERMAIN_H +#define DESIGNPATTERN_ADAPTERMAIN_H + +#include "adapter.h" + +void AdapterMain() +{ + // + IRussiaSocket * pAdapter = new PowerAdapter(); + + // + pAdapter->Charge(); + + SAFE_DELETE(pAdapter); +} + +#endif //DESIGNPATTERN_ADAPTERMAIN_H diff --git a/DesignPattern/AdapterPattern/adaptee.h b/DesignPattern/AdapterPattern/adaptee.h new file mode 100644 index 0000000..679c378 --- /dev/null +++ b/DesignPattern/AdapterPattern/adaptee.h @@ -0,0 +1,20 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#ifndef DESIGNPATTERN_ADAPTEE_H +#define DESIGNPATTERN_ADAPTEE_H + +#include + +// 自带的充电器(两脚扁型) +class OwnCharger +{ +public: + void ChargeWithFeetFlat() + { + std::cout << "OwnCharger::ChargeWithFeetFlat\n"; + } +}; + +#endif //DESIGNPATTERN_ADAPTEE_H diff --git a/DesignPattern/AdapterPattern/adapter.h b/DesignPattern/AdapterPattern/adapter.h new file mode 100644 index 0000000..ecb94f5 --- /dev/null +++ b/DesignPattern/AdapterPattern/adapter.h @@ -0,0 +1,34 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#ifndef DESIGNPATTERN_ADAPTER_H +#define DESIGNPATTERN_ADAPTER_H + +#include "target.h" +#include "adaptee.h" + +#ifndef SAFE_DELETE +#define SAFE_DELETE(p) { if(p){delete(p); (p)=NULL;} } +#endif + +// 电源适配器 +class PowerAdapter : public IRussiaSocket +{ +public: + PowerAdapter() : m_pCharger(new OwnCharger()){} + ~PowerAdapter() + { + SAFE_DELETE(m_pCharger); + } + void Charge() + { + // 使用自带的充电器(两脚扁形)充电 + m_pCharger->ChargeWithFeetFlat(); + } +private: + // 持有需要被适配的接口对象(自带的充电器) + OwnCharger* m_pCharger; +}; + +#endif //DESIGNPATTERN_ADAPTER_H diff --git a/DesignPattern/AdapterPattern/target.h b/DesignPattern/AdapterPattern/target.h new file mode 100644 index 0000000..cbe7cfa --- /dev/null +++ b/DesignPattern/AdapterPattern/target.h @@ -0,0 +1,16 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#ifndef DESIGNPATTERN_TARGET_H +#define DESIGNPATTERN_TARGET_H + +// 俄罗斯提供的插座 +class IRussiaSocket +{ +public: + // 使用双脚圆形充电(暂不实现) + virtual void Charge() = 0; +}; + +#endif //DESIGNPATTERN_TARGET_H diff --git a/DesignPattern/BridgePattern/BridgeMain.cpp b/DesignPattern/BridgePattern/BridgeMain.cpp new file mode 100644 index 0000000..00324cd --- /dev/null +++ b/DesignPattern/BridgePattern/BridgeMain.cpp @@ -0,0 +1,30 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#include "BridgeMain.h" + +void BridgeMain() +{ + // ơȣ + IElectricalEquipment * light = new Light(); + IElectricalEquipment * fan = new Fan(); + + // أʽءλأ + // ʽغ͵ƹλغͷȹ + ISwitch * pullChain = new PullChainSwitch(light); + ISwitch * twoPosition = new TwoPositionSwitch(fan); + + // ơص + pullChain->On(); + pullChain->Off(); + + // 򿪷ȡرշ + twoPosition->On(); + twoPosition->Off(); + + SAFE_DELETE(twoPosition); + SAFE_DELETE(pullChain); + SAFE_DELETE(fan); + SAFE_DELETE(light); +} \ No newline at end of file diff --git a/DesignPattern/BridgePattern/BridgeMain.h b/DesignPattern/BridgePattern/BridgeMain.h new file mode 100644 index 0000000..01ef6eb --- /dev/null +++ b/DesignPattern/BridgePattern/BridgeMain.h @@ -0,0 +1,17 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#ifndef DESIGNPATTERN_BRIDGEMAIN_H +#define DESIGNPATTERN_BRIDGEMAIN_H + +#include "refined_abstraction.h" +#include "concrete_implementor.h" + +#ifndef SAFE_DELETE +#define SAFE_DELETE(p) { if(p){delete(p); (p)=nullptr;} } +#endif + +void BridgeMain(); + +#endif //DESIGNPATTERN_BRIDGEMAIN_H diff --git a/DesignPattern/BridgePattern/abstraction.h b/DesignPattern/BridgePattern/abstraction.h new file mode 100644 index 0000000..82cdd59 --- /dev/null +++ b/DesignPattern/BridgePattern/abstraction.h @@ -0,0 +1,23 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#ifndef DESIGNPATTERN_ABSTRACTION_H +#define DESIGNPATTERN_ABSTRACTION_H + +#include "implementor.h" + +// 开关 +class ISwitch +{ +public: + ISwitch(IElectricalEquipment *ee){ m_pEe = ee; } + virtual ~ISwitch(){} + virtual void On() = 0; // 打开电器 + virtual void Off() = 0; // 关闭电器 + +protected: + IElectricalEquipment * m_pEe; +}; + +#endif //DESIGNPATTERN_ABSTRACTION_H diff --git a/DesignPattern/BridgePattern/concrete_implementor.h b/DesignPattern/BridgePattern/concrete_implementor.h new file mode 100644 index 0000000..35a2daa --- /dev/null +++ b/DesignPattern/BridgePattern/concrete_implementor.h @@ -0,0 +1,44 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#ifndef DESIGNPATTERN_CONCRETE_IMPLEMENTOR_H +#define DESIGNPATTERN_CONCRETE_IMPLEMENTOR_H + +#include "implementor.h" +#include + +// 电灯 +class Light : public IElectricalEquipment +{ +public: + // 开灯 + virtual void PowerOn() override + { + std::cout << "Light is on." << std::endl; + } + // 关灯 + virtual void PowerOff() override + { + std::cout << "Light is off." << std::endl; + } +}; + +// 风扇 +class Fan : public IElectricalEquipment +{ +public: + // 打开风扇 + virtual void PowerOn() override + { + std::cout << "Fan is on." << std::endl; + } + //关闭风扇 + virtual void PowerOff() override + { + std::cout << "Fan is off." << std::endl; + } +}; + + +#endif //DESIGNPATTERN_CONCRETE_IMPLEMENTOR_H diff --git a/DesignPattern/BridgePattern/implementor.h b/DesignPattern/BridgePattern/implementor.h new file mode 100644 index 0000000..c8b801e --- /dev/null +++ b/DesignPattern/BridgePattern/implementor.h @@ -0,0 +1,17 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#ifndef DESIGNPATTERN_IMPLEMENTOR_H +#define DESIGNPATTERN_IMPLEMENTOR_H + +// 电器 +class IElectricalEquipment +{ +public: + virtual ~IElectricalEquipment(){} + virtual void PowerOn() = 0; // 打开 + virtual void PowerOff() = 0; // 关闭 +}; + +#endif //DESIGNPATTERN_IMPLEMENTOR_H diff --git a/DesignPattern/BridgePattern/refined_abstraction.h b/DesignPattern/BridgePattern/refined_abstraction.h new file mode 100644 index 0000000..48be107 --- /dev/null +++ b/DesignPattern/BridgePattern/refined_abstraction.h @@ -0,0 +1,53 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#ifndef DESIGNPATTERN_REFINED_ABSTRACTION_H +#define DESIGNPATTERN_REFINED_ABSTRACTION_H + +#include "abstraction.h" +#include + +// 拉链式开关 +class PullChainSwitch : public ISwitch +{ +public: + PullChainSwitch(IElectricalEquipment *ee) : ISwitch(ee) {} + + // 用拉链式开关打开电器 + virtual void On() override + { + std::cout << "Switch on the equipment with a pull chain switch." << std::endl; + m_pEe->PowerOn(); + } + + // 用拉链式开关关闭电器 + virtual void Off() override + { + std::cout << "Switch off the equipment with a pull chain switch." << std::endl; + m_pEe->PowerOff(); + } +}; + +// 两位开关 +class TwoPositionSwitch : public ISwitch +{ +public: + TwoPositionSwitch(IElectricalEquipment *ee) : ISwitch(ee) {} + + // 用两位开关打开电器 + virtual void On() override + { + std::cout << "Switch on the equipment with a two-position switch." << std::endl; + m_pEe->PowerOn(); + } + + // 用两位开关关闭电器 + virtual void Off() override { + std::cout << "Switch off the equipment with a two-position switch." << std::endl; + m_pEe->PowerOff(); + } +}; + + +#endif //DESIGNPATTERN_REFINED_ABSTRACTION_H diff --git a/DesignPattern/CMakeLists.txt b/DesignPattern/CMakeLists.txt new file mode 100644 index 0000000..4fdbffe --- /dev/null +++ b/DesignPattern/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.10) +project(DesignPattern) + +set(CMAKE_CXX_STANDARD 11) + +add_executable(DesignPattern main.cpp AbstractFactoryPattern/product.h AbstractFactoryPattern/concrete_product.h AbstractFactoryPattern/Factory.cpp AbstractFactoryPattern/Factory.h AbstractFactoryPattern/concrete_factory.h AbstractFactoryPattern/FactoryMain.cpp AbstractFactoryPattern/FactoryMain.h SingletonPattern/Singleton.cpp SingletonPattern/Singleton.h SingletonPattern/SingletonMain.h AdapterPattern/target.h AdapterPattern/adaptee.h AdapterPattern/adapter.h AdapterPattern/AdapterMain.h BridgePattern/implementor.h BridgePattern/concrete_implementor.h BridgePattern/abstraction.h BridgePattern/refined_abstraction.h BridgePattern/BridgeMain.h BridgePattern/BridgeMain.cpp ObserverPattern/subject.h ObserverPattern/observer.h ObserverPattern/concrete_subject.h ObserverPattern/concrete_observer.h ObserverPattern/ObserverMain.cpp ObserverPattern/ObserverMain.h) \ No newline at end of file diff --git a/DesignPattern/ObserverPattern/ObserverMain.cpp b/DesignPattern/ObserverPattern/ObserverMain.cpp new file mode 100644 index 0000000..cd536c7 --- /dev/null +++ b/DesignPattern/ObserverPattern/ObserverMain.cpp @@ -0,0 +1,34 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#include "ObserverMain.h" + +void ObserverMain() +{ + // + ConcreteSubject * pSubject = new ConcreteSubject(); + + // ۲ + IObserver * pObserver1 = new ConcreteObserver("Jack Ma"); + IObserver * pObserver2 = new ConcreteObserver("Pony"); + + // ע۲ + pSubject->Attach(pObserver1); + pSubject->Attach(pObserver2); + + // ļ۸񣬲֪ͨ۲ + pSubject->SetPrice(12.5); + pSubject->Notify(); + + // עһ۲ + pSubject->Detach(pObserver2); + + // ٴθ״̬֪ͨ۲ + pSubject->SetPrice(15.0); + pSubject->Notify(); + + SAFE_DELETE(pObserver1); + SAFE_DELETE(pObserver2); + SAFE_DELETE(pSubject); +} \ No newline at end of file diff --git a/DesignPattern/ObserverPattern/ObserverMain.h b/DesignPattern/ObserverPattern/ObserverMain.h new file mode 100644 index 0000000..146c07c --- /dev/null +++ b/DesignPattern/ObserverPattern/ObserverMain.h @@ -0,0 +1,17 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#ifndef DESIGNPATTERN_OBSERVERMAIN_H +#define DESIGNPATTERN_OBSERVERMAIN_H + +#include "concrete_subject.h" +#include "concrete_observer.h" + +#ifndef SAFE_DELETE +#define SAFE_DELETE(p) { if(p){delete(p); (p)=nullptr;} } +#endif + +void ObserverMain(); + +#endif //DESIGNPATTERN_OBSERVERMAIN_H diff --git a/DesignPattern/ObserverPattern/concrete_observer.h b/DesignPattern/ObserverPattern/concrete_observer.h new file mode 100644 index 0000000..37a68b3 --- /dev/null +++ b/DesignPattern/ObserverPattern/concrete_observer.h @@ -0,0 +1,25 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#ifndef DESIGNPATTERN_CONCRETE_OBSERVER_H +#define DESIGNPATTERN_CONCRETE_OBSERVER_H + +#include "observer.h" +#include +#include + +class ConcreteObserver : public IObserver +{ +public: + ConcreteObserver(std::string name) { m_strName = name; } + void Update(float price) + { + std::cout << m_strName << " - price" << price << "\n"; + } + +private: + std::string m_strName; // 名字 +}; + +#endif //DESIGNPATTERN_CONCRETE_OBSERVER_H diff --git a/DesignPattern/ObserverPattern/concrete_subject.h b/DesignPattern/ObserverPattern/concrete_subject.h new file mode 100644 index 0000000..d05e98c --- /dev/null +++ b/DesignPattern/ObserverPattern/concrete_subject.h @@ -0,0 +1,45 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#ifndef DESIGNPATTERN_CONCRETE_SUBJECT_H +#define DESIGNPATTERN_CONCRETE_SUBJECT_H + +#include "subject.h" +#include "observer.h" +#include +#include + +// 具体主题 +class ConcreteSubject : public ISubject +{ +public: + ConcreteSubject(){ m_fPrice = 10.0; } + void SetPrice(float price) + { + m_fPrice = price; + } + void Attach(IObserver * observer) + { + m_observers.push_back(observer); + } + void Detach(IObserver * observer) + { + m_observers.remove(observer); + } + // 通知所有观察者 + void Notify() + { + std::list::iterator it = m_observers.begin(); + while (it != m_observers.end()) + { + (*it)->Update(m_fPrice); + ++it; + } + } +private: + std::list m_observers; // 观察者列表 + float m_fPrice; // 价格 +}; + +#endif //DESIGNPATTERN_CONCRETE_SUBJECT_H diff --git a/DesignPattern/ObserverPattern/observer.h b/DesignPattern/ObserverPattern/observer.h new file mode 100644 index 0000000..500c396 --- /dev/null +++ b/DesignPattern/ObserverPattern/observer.h @@ -0,0 +1,15 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#ifndef DESIGNPATTERN_OBSERVER_H +#define DESIGNPATTERN_OBSERVER_H + +// 抽象观察者 +class IObserver +{ +public: + virtual void Update(float price) = 0; // 更新价格 +}; + +#endif //DESIGNPATTERN_OBSERVER_H diff --git a/DesignPattern/ObserverPattern/subject.h b/DesignPattern/ObserverPattern/subject.h new file mode 100644 index 0000000..9d50fba --- /dev/null +++ b/DesignPattern/ObserverPattern/subject.h @@ -0,0 +1,18 @@ +// +// Created by xiemenghui on 2018/7/21. +// + +#ifndef DESIGNPATTERN_SUBJECT_H +#define DESIGNPATTERN_SUBJECT_H + +class IObserver; + +class ISubject +{ +public: + virtual void Attach(IObserver *) = 0; // 注册观察者 + virtual void Detach(IObserver *) = 0; // 注销观察者 + virtual void Notify() = 0; // 通知观察者 +}; + +#endif //DESIGNPATTERN_SUBJECT_H diff --git a/DesignPattern/README.md b/DesignPattern/README.md new file mode 100644 index 0000000..7044985 --- /dev/null +++ b/DesignPattern/README.md @@ -0,0 +1 @@ +# DesignPattern diff --git a/DesignPattern/SingletonPattern/README.md b/DesignPattern/SingletonPattern/README.md new file mode 100644 index 0000000..580b0b2 --- /dev/null +++ b/DesignPattern/SingletonPattern/README.md @@ -0,0 +1,34 @@ +# 单例模式 + +```cpp +// 懒汉式单例模式 +class Singleton +{ +private: + Singleton() { } + static Singleton * pInstance; +public: + static Singleton * GetInstance() + { + if (pInstance == nullptr) + pInstance = new Singleton(); + return pInstance; + } +}; + +// 线程安全的单例模式 +class Singleton +{ +private: + Singleton() { } + ~Singleton() { } + Singleton(const Singleton &); + Singleton & operator = (const Singleton &); +public: + static Singleton & GetInstance() + { + static Singleton instance; + return instance; + } +}; +``` \ No newline at end of file diff --git a/DesignPattern/SingletonPattern/Singleton.cpp b/DesignPattern/SingletonPattern/Singleton.cpp new file mode 100644 index 0000000..f090e53 --- /dev/null +++ b/DesignPattern/SingletonPattern/Singleton.cpp @@ -0,0 +1,11 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#include +#include "Singleton.h" + +void Singleton::DoSomething() +{ + std::cout << "Singleton do something\n"; +} \ No newline at end of file diff --git a/DesignPattern/SingletonPattern/Singleton.h b/DesignPattern/SingletonPattern/Singleton.h new file mode 100644 index 0000000..79bc8ad --- /dev/null +++ b/DesignPattern/SingletonPattern/Singleton.h @@ -0,0 +1,25 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#ifndef DESIGNPATTERN_SINGLETON_H +#define DESIGNPATTERN_SINGLETON_H + +// 单例模式 +class Singleton { +private: + Singleton(){} + ~Singleton(){} + Singleton(const Singleton &); + Singleton & operator= (const Singleton &); + +public: + static Singleton & GetInstance() + { + static Singleton instance; + return instance; + } + void DoSomething(); +}; + +#endif //DESIGNPATTERN_SINGLETON_H diff --git a/DesignPattern/SingletonPattern/SingletonMain.h b/DesignPattern/SingletonPattern/SingletonMain.h new file mode 100644 index 0000000..673860f --- /dev/null +++ b/DesignPattern/SingletonPattern/SingletonMain.h @@ -0,0 +1,15 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#ifndef DESIGNPATTERN_SINGLETONMAIN_H +#define DESIGNPATTERN_SINGLETONMAIN_H + +#include "Singleton.h" + +void SingletonMain() +{ + Singleton::GetInstance().DoSomething(); +} + +#endif //DESIGNPATTERN_SINGLETONMAIN_H diff --git a/DesignPattern/main.cpp b/DesignPattern/main.cpp new file mode 100644 index 0000000..9e646ed --- /dev/null +++ b/DesignPattern/main.cpp @@ -0,0 +1,43 @@ +// +// Created by xiemenghui on 2018/7/20. +// + +#include +#include "SingletonPattern/SingletonMain.h" +#include "AbstractFactoryPattern/FactoryMain.h" +#include "AdapterPattern/AdapterMain.h" +#include "BridgePattern/BridgeMain.h" +#include "ObserverPattern/ObserverMain.h" + +int main() { + std::cout << "*******************" << std::endl; + std::cout << "** ģʽ **" << std::endl; + std::cout << "*******************" << std::endl; + + std::cout << "*******************" << std::endl; + std::cout << "** ģʽ **" << std::endl; + std::cout << "*******************" << std::endl; + SingletonMain(); + + std::cout << "*******************" << std::endl; + std::cout << "** 󹤳ģʽ **" << std::endl; + std::cout << "*******************" << std::endl; + FactoryMain(); + + std::cout << "*******************" << std::endl; + std::cout << "** ģʽ **" << std::endl; + std::cout << "*******************" << std::endl; + AdapterMain(); + + std::cout << "*******************" << std::endl; + std::cout << "** Žģʽ **" << std::endl; + std::cout << "*******************" << std::endl; + BridgeMain(); + + std::cout << "*******************" << std::endl; + std::cout << "** ۲ģʽ **" << std::endl; + std::cout << "*******************" << std::endl; + ObserverMain(); + + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index cde865d..6704667 100644 --- a/README.md +++ b/README.md @@ -2420,6 +2420,8 @@ ssize_t write(int fd, const void *buf, size_t count); ## 设计模式 +> 各大设计模式例子参考:[CSDN专栏 . C++ 设计模式](https://blog.csdn.net/column/details/15392.html) 系列博文 + ### 设计模式的六大原则 * 单一职责原则(SRP,Single Responsibility Principle) @@ -2431,38 +2433,23 @@ ssize_t write(int fd, const void *buf, size_t count); ### 单例模式 -```cpp -// 懒汉式单例模式 -class Singleton -{ -private: - Singleton() { } - static Singleton * pInstance; -public: - static Singleton * GetInstance() - { - if (pInstance == nullptr) - pInstance = new Singleton(); - return pInstance; - } -}; +[单例模式例子](DesignPattern/SingletonPattern) -// 线程安全的单例模式 -class Singleton -{ -private: - Singleton() { } - ~Singleton() { } - Singleton(const Singleton &); - Singleton & operator = (const Singleton &); -public: - static Singleton & GetInstance() - { - static Singleton instance; - return instance; - } -}; -``` +### 抽象工厂模式 + +[抽象工厂模式例子](DesignPattern/AbstractFactoryPattern) + +### 适配器模式 + +[适配器模式例子](DesignPattern/AdapterPattern) + +### 桥接模式 + +[桥接模式例子](DesignPattern/BridgePattern) + +### 观察者模式 + +[观察者模式例子](DesignPattern/ObserverPattern) ## 链接装载库