简单工厂模式

实现了客户端与功能类的解耦 但是违反了类的单一职责原则,整个类的代码冗长,阅读难度、维护难度和测试难度也很大 违反了开闭原则,增添功能要修改源代码 适用场景: 工厂类负责创建的对象比较少,不会造成工厂方法中的业务逻辑太过复杂 客户端只知道传入工厂类的参数,对于如何创建对象并不关心 // 简单工厂模式 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

合成复用原则

优先使用组合的方式,而不是继承 class AbstractCar{ public: virtual void run() = 0; }; class BMW:public AbstractCar{ public: void run()override{ cout << "BMW run" << endl; } }; class DaZhong:public AbstractCar{ public: void run()override{ cout << "DaZhong run" << endl; } }; #if 0// 错误的做法 class Person : public BMW{ public: void drive(){ run(); } }; void test1(){ Person p; p.drive(); } #endif // 正确做法 class Person{ public: Person(){ car = nullptr; } Person(AbstractCar* c){ car = c; } void drive(){ car->run(); delete car; } void drive(AbstractCar* car){ car->run(); delete car; } private: AbstractCar* car; }; void test2(){ // 方式一: Person* p = new Person(new BMW); p->drive(); delete p; p = new Person(new DaZhong); p->drive(); delete p; // 方式二: Person pp; pp....

January 30, 2022 · 1 min · Rick Cui

迪米特法则

又叫最少知识原则 封装一个中间层进行隔离 class AbstractBuilding{ public: virtual string getQuality()const = 0; virtual void sale() = 0; }; class BuildingA:public AbstractBuilding{ public: BuildingA(){ m_quality = "高品质"; } string getQuality()const override{ return m_quality; } void sale()override{ cout << "楼盘A:" << m_quality << endl; } private: string m_quality; }; class BuildingB:public AbstractBuilding{ public: BuildingB(){ m_quality = "低品质"; } string getQuality()const override{ return m_quality; } void sale()override{ cout << "楼盘B:" << m_quality << endl; } private: string m_quality; }; // 错误的做法 // 客户端 void test1(){ string myRequest = "低品质"; BuildingA* bA = new BuildingA; if(bA->getQuality() == myRequest){ bA->sale(); delete bA; } BuildingB* bB = new BuildingB; if(bB->getQuality() == myRequest){ bB->sale(); delete bB; } } // 正确做法 // 封装一个中间类 class Mediator{ public: Mediator(){ AbstractBuilding* b = new BuildingA; vb....

January 30, 2022 · 1 min · Rick Cui

开闭原则

对扩展开放,对修改关闭 类模块应该是可扩展的,但是不可修改源代码 如下所示,如果想要扩展乘法、除法、取模等算法,就不用修改原码,只需增加新类 class AbstractCalculator{ public: virtual void setOperatorNum(int a, int b) = 0; virtual int getResult() = 0; }; class PlusCalculator:public AbstractCalculator{ public: void setOperatorNum(int a, int b)override{ m_a = a; m_b = b; } int getResult()override{ return m_a + m_b; } private: int m_a{0}; int m_b{0}; }; class MinusCalculator:public AbstractCalculator{ public: void setOperatorNum(int a, int b)override{ m_a = a; m_b = b; } int getResult()override{ return m_a - m_b; } private: int m_a{0}; int m_b{0}; }; int main() { AbstractCalculator* calculator = new PlusCalculator; calculator->setOperatorNum(10, 20); cout << calculator->getResult() << endl; delete calculator; calculator = nullptr; calculator = new MinusCalculator; calculator->setOperatorNum(10, 20); cout << calculator->getResult() << endl; delete calculator; calculator = nullptr; return 0; }

January 30, 2022 · 1 min · Rick Cui