有一个已经写好的接口,但是不符合需求,适配器就是将已有的接口转化为需要的形式
class MyPrint{ public: void operator()(int a, int b){ cout << a + b << endl; } }; class TargetInterface{ public: virtual void operator()(int v) = 0; }; class MyAdaptor : public TargetInterface{ public: MyAdaptor(int param):param(param){} void operator()(int v){ print(v, param); } private: MyPrint print; int param; }; MyAdaptor myBind2nd(int v){ return MyAdaptor(v); } int main() { vector<int> v; for(int i = 0; i < 10; ++i){ v.push_back(i); } for_each(v.begin(), v....
将复杂的子系统抽象到同一个接口进行管理,外界只需要通过此接口与子类系统进行交互,而不必直接与复杂的子类系统进行交互
案例:家庭影院外观模式 实现KTV模式:电视打开,灯关掉,音响打开,麦克风打开,DVD打开
实现游戏模式:电视打开,音响打开,游戏机打开
为其他对象提供一种代理以控制对这个对象的访问。在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用。
单例在内存中只有一份,在内存中不会占用太多,程序退出时会随着程序统一释放内存,所以可以不必特意释放 一、懒汉式 不是线程安全的,所以多线程使用要加锁
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()....
抽象工厂针对的是产品族,而不是产品等级 产品族:同一产地或者同一厂商,功能不同 产品等级:功能相同,产地或者厂商不同 对于增加产品族,符合开闭原则 对于增加产品等级,不符合开闭原则 // 抽象工厂模式 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; }