单例模式

单例在内存中只有一份,在内存中不会占用太多,程序退出时会随着程序统一释放内存,所以可以不必特意释放 一、懒汉式 不是线程安全的,所以多线程使用要加锁 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

简单工厂模式

实现了客户端与功能类的解耦 但是违反了类的单一职责原则,整个类的代码冗长,阅读难度、维护难度和测试难度也很大 违反了开闭原则,增添功能要修改源代码 适用场景: 工厂类负责创建的对象比较少,不会造成工厂方法中的业务逻辑太过复杂 客户端只知道传入工厂类的参数,对于如何创建对象并不关心 // 简单工厂模式 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 FruitFactory{ public: static AbstractFruit* CreateFruit(string flag){ if(flag == "apple"){ return new Apple; } else if(flag == "banana"){ return new Banana; } else if(flag == "pear"){ return new Pear; } else{ return nullptr; } } }; int main() { AbstractFruit* fruit = FruitFactory::CreateFruit("apple"); fruit->showName(); delete fruit; fruit = FruitFactory::CreateFruit("banana"); fruit->showName(); delete fruit; fruit = FruitFactory::CreateFruit("pear"); fruit->showName(); delete fruit; return 0; }

January 31, 2022 · 1 min · Rick Cui

依赖倒置原则

传统过程式设计 传统的过程式设计倾向于使高层次的模块依赖于低层次的模块,抽象层依赖于具体实现层 // 底层实现 class BankWorker{ public: void saveService(){ cout << "办理存款业务..." << endl; } void transferService(){ cout << "办理转账业务..." << endl; } void payService(){ cout << "办理支付业务..." << endl; } }; // 中层封装 void doSaveBusiness(BankWorker* worker){ worker->saveService(); } void doTransferBusiness(BankWorker* worker){ worker->transferService(); } void doPayBusiness(BankWorker* worker){ worker->payService(); } // 高层调用 void test1(){ BankWorker* bw = new BankWorker; doSaveBusiness(bw); doPayBusiness(bw); doTransferBusiness(bw); delete bw; } 依赖倒置 高层模块(稳定) 不应该依赖于 低层模块(变化),二者都应该依赖于 抽象(稳定) 抽象(稳定) 不应该依赖于 实现细节(变化),实现细节应该依赖于 抽象(稳定) // 抽象层 class AbstractWorker{ public: virtual void doBusiness() = 0; }; // 实现层 class SaveBankWorker:public AbstractWorker{ public: void doBusiness()override{ cout << "办理存款业务....

January 31, 2022 · 1 min · Rick Cui