工厂方法模式 = 简单工厂模式 + 开闭原则
类的个数成倍增多,维护成本很高
适用场景:
客户端不知道它所需要的对象的类 抽象工厂类通过其子类来指定创建哪个对象 // 工厂方法模式 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; }
实现了客户端与功能类的解耦 但是违反了类的单一职责原则,整个类的代码冗长,阅读难度、维护难度和测试难度也很大 违反了开闭原则,增添功能要修改源代码 适用场景:
工厂类负责创建的对象比较少,不会造成工厂方法中的业务逻辑太过复杂 客户端只知道传入工厂类的参数,对于如何创建对象并不关心 // 简单工厂模式 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; }
传统过程式设计 传统的过程式设计倾向于使高层次的模块依赖于低层次的模块,抽象层依赖于具体实现层
// 底层实现 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 << "办理存款业务....
优先使用组合的方式,而不是继承
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....
又叫最少知识原则
封装一个中间层进行隔离
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....