工厂方法模式(Factory Method),定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使用一个类的实例化延迟到其子类。根据依赖倒转原则,我们把工厂类抽象出一个接口,这个接口只有一个方法,就是创建抽象产品的工厂方法。然后,所有的要生产具体类的工厂,就去实现这个接口,这样,一个简单工厂模式的工厂类,变成了一个工厂抽象接口和多个具体生成对象的工厂。我们在要增加新的功能,就不需要更改原有的工厂类了,只需要增加此功能的运算类和相应的工厂类就可以了。这样整个工厂和产品系其实都没有修改的变化,而只有扩展的变化,这就完全符合了开发-封闭原则。
工厂方法模式实现时,客户端需要决定实例化哪一个工厂来实现运算类,选择判断的问题还是存在的,也就是说,工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行。你想要加功能,本来是改工厂类的,而现在是修改客户端。
简单工厂模式的最大有点在于工厂类中包含了必要的逻辑判断,根据客户端的选择条件动态实例化相关的类,对于客户端来说,去除了与具体产品的依赖。但是简单工厂模式违背了开放-封闭原则。而工厂方法克服了违背开放-封闭原则的缺点,又保持了封闭对象创建过程的优点,他们集中封装对象的创建,使得要更换对象时,不需要做大的改动就可实现,降低了客户程序和产品对象的耦合。工厂方法模式是简单工厂模式的进一步抽象和推广。由于使用了多态性,工厂方法模式保持了简单工厂模式的优点,而且克服了它的缺点。但缺点是由于每增加一个产品,就需要加一个产品工厂的类,增加了额外的开发量。
#include <iostream> using namespace std; class Operation { private: double _numberA; double _numberB; public: Operation(){} Operation(double numA,double numB):_numberA(numA),_numberB(numB){} void set_numberA(double numA) { _numberA=numA; } double get_numberA() { return _numberA; } void set_numberB(double numB) { _numberB=numB; } double get_numberB() { return _numberB; } virtual double GetResult() { double result=0; return result; } }; //加减乘除类 class OperationAdd:public Operation { public: OperationAdd(){} OperationAdd(double numA,double numB):Operation(numA,numB){} double GetResult() { double result=0; result=get_numberA()+get_numberB(); return result; } }; class OperationSub:public Operation { public: OperationSub(){} OperationSub(double numA,double numB):Operation(numA,numB){} double GetResult() { double result=0; result=get_numberA()-get_numberB(); return result; } }; class OperationMul:public Operation { public: OperationMul(){} OperationMul(double numA,double numB):Operation(numA,numB){} double GetResult() { double result=0; result=get_numberA()*get_numberB(); return result; } }; class OperationDiv:public Operation { public: OperationDiv(){} OperationDiv(double numA,double numB):Operation(numA,numB){} double GetResult() { double result=0; if(get_numberB()==0) { cout << "除数不能为0" << endl; return -1; } result=get_numberA()/get_numberB(); return result; } }; //工厂接口 class IFactory { public: virtual Operation *CreateOperation()=0; }; //加减乘除工厂类 class AddFactory:public IFactory,public Operation { public: AddFactory(){} AddFactory(double numA,double numB):Operation(numA,numB){} Operation *CreateOperation() { return new OperationAdd; } }; class SubFactory:public IFactory,public Operation { public: SubFactory(){} SubFactory(double numA,double numB):Operation(numA,numB){} Operation *CreateOperation() { return new OperationMul; } }; class MulFactory:public IFactory,public Operation { public: MulFactory(){} MulFactory(double numA,double numB):Operation(numA,numB){} Operation *CreateOperation() { return new OperationMul; } }; class DivFactory:public IFactory,public Operation { public: DivFactory(){} DivFactory(double numA,double numB):Operation(numA,numB){} Operation *CreateOperation() { return new OperationDiv; } }; int main() { IFactory *operFactory=new AddFactory; Operation *oper=operFactory->CreateOperation(); oper->set_numberA(1); oper->set_numberB(2); double result=oper->GetResult(); cout << "result=" << result; return 0; }