外观模式

将复杂的子系统抽象到同一个接口进行管理,外界只需要通过此接口与子类系统进行交互,而不必直接与复杂的子类系统进行交互 案例:家庭影院外观模式 实现KTV模式:电视打开,灯关掉,音响打开,麦克风打开,DVD打开 实现游戏模式:电视打开,音响打开,游戏机打开

February 1, 2022 · 1 min · Rick Cui

代理模式

为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。

February 1, 2022 · 1 min · Rick Cui

单例模式

单例在内存中只有一份,在内存中不会占用太多,程序退出时会随着程序统一释放内存,所以可以不必特意释放 一、懒汉式 不是线程安全的,所以多线程使用要加锁 class Singleton{ public: static Singleton* getInstance(){ if(s_instance == nullptr){ s_instance = new Singleton; } return s_instance; } // 可有可无,程序结束时,系统会自动回收程序的所有内存 class Garbo{ ~Garbo(){ if(s_instance){ cout << "gc..." << endl; delete s_instance; } } }; private: Singleton(){ cout << "Singleton()..." << endl; } Singleton(const Singleton&) = delete; Singleton& operator=(const Singleton&) = delete; static Singleton* s_instance; static Garbo s_gc; }; Singleton* Singleton::s_instance = nullptr; int main() { cout << "main()....

January 31, 2022 · 2 min · Rick Cui

抽象工厂模式

抽象工厂针对的是产品族,而不是产品等级 产品族:同一产地或者同一厂商,功能不同 产品等级:功能相同,产地或者厂商不同 对于增加产品族,符合开闭原则 对于增加产品等级,不符合开闭原则 // 抽象工厂模式 class AbstractApple{ public: virtual void showName() = 0; }; class AbstractBanana{ public: virtual void showName() = 0; }; class AbstractPear{ public: virtual void showName() = 0; }; class ChinaApple : public AbstractApple{ public: void showName() override{ cout << "我是中国苹果!" << endl; } }; class ChinaBanana : public AbstractBanana{ public: void showName() override{ cout << "我是中国香蕉!" << endl; } }; class ChinaPear : public AbstractPear{ public: void showName() override{ cout << "我是中国鸭梨!" << endl; } }; class USAApple : public AbstractApple{ public: void showName() override{ cout << "我是美国苹果!" << endl; } }; class USABanana : public AbstractBanana{ public: void showName() override{ cout << "我是美国香蕉!" << endl; } }; class USAPear : public AbstractPear{ public: void showName() override{ cout << "我是美国鸭梨!" << endl; } }; class JapanApple : public AbstractApple{ public: void showName() override{ cout << "我是日本苹果!" << endl; } }; class JapanBanana : public AbstractBanana{ public: void showName() override{ cout << "我是日本香蕉!" << endl; } }; class JapanPear : public AbstractPear{ public: void showName() override{ cout << "我是日本鸭梨!" << endl; } }; class AbstractFactory{ public: virtual AbstractApple* CreateApple() = 0; virtual AbstractBanana* CreateBanana() = 0; virtual AbstractPear* CreatePear() = 0; }; class ChinaFactory : public AbstractFactory{ public: AbstractApple* CreateApple() override{ return new ChinaApple; } AbstractBanana* CreateBanana() override{ return new ChinaBanana; } AbstractPear* CreatePear() override{ return new ChinaPear; } }; class USAFactory : public AbstractFactory{ public: AbstractApple* CreateApple() override{ return new USAApple; } AbstractBanana* CreateBanana() override{ return new USABanana; } AbstractPear* CreatePear() override{ return new USAPear; } }; class JapanFactory : public AbstractFactory{ public: AbstractApple* CreateApple() override{ return new JapanApple; } AbstractBanana* CreateBanana() override{ return new JapanBanana; } AbstractPear* CreatePear() override{ return new JapanPear; } }; int main() { AbstractFactory* factory = nullptr; AbstractApple* apple = nullptr; AbstractBanana* banana = nullptr; AbstractPear* pear = nullptr; // 中国 factory = new ChinaFactory; apple = factory->CreateApple(); apple->showName(); banana = factory->CreateBanana(); banana->showName(); pear = factory->CreatePear(); pear->showName(); delete pear; delete banana; delete apple; delete factory; // 美国 factory = new USAFactory; apple = factory->CreateApple(); apple->showName(); banana = factory->CreateBanana(); banana->showName(); pear = factory->CreatePear(); pear->showName(); delete pear; delete banana; delete apple; delete factory; // 日本 factory = new JapanFactory; apple = factory->CreateApple(); apple->showName(); banana = factory->CreateBanana(); banana->showName(); pear = factory->CreatePear(); pear->showName(); delete pear; delete banana; delete apple; delete factory; return 0; }

January 31, 2022 · 2 min · Rick Cui

工厂方法模式

工厂方法模式 = 简单工厂模式 + 开闭原则 类的个数成倍增多,维护成本很高 适用场景: 客户端不知道它所需要的对象的类 抽象工厂类通过其子类来指定创建哪个对象 // 工厂方法模式 class AbstractFruit{ public: virtual void showName() = 0; }; class Apple : public AbstractFruit{ public: void showName() override{ cout << "我是苹果!" << endl; } }; class Banana : public AbstractFruit{ public: void showName() override{ cout << "我是香蕉!" << endl; } }; class Pear : public AbstractFruit{ public: void showName() override{ cout << "我是鸭梨!" << endl; } }; // 抽象工厂 class AbstractFruitFactory{ public: virtual AbstractFruit* CreateFruit() = 0; }; class AppleFactory : public AbstractFruitFactory{ public: AbstractFruit* CreateFruit() override{ return new Apple; } }; class PearFactory : public AbstractFruitFactory{ public: AbstractFruit* CreateFruit() override{ return new Pear; } }; class BananaFactory : public AbstractFruitFactory{ public: AbstractFruit* CreateFruit() override{ return new Banana; } }; int main() { AbstractFruitFactory* factory = nullptr; AbstractFruit* fruit = nullptr; factory = new AppleFactory; fruit = factory->CreateFruit(); fruit->showName(); delete fruit; delete factory; factory = new BananaFactory; fruit = factory->CreateFruit(); fruit->showName(); delete fruit; delete factory; factory = new PearFactory; fruit = factory->CreateFruit(); fruit->showName(); delete fruit; delete factory; return 0; }

January 31, 2022 · 1 min · Rick Cui