简单工厂模式:
一个工厂,多个产品。产品需要有一个虚基类。通过传入参数,生成具体产品对象,并利用基类指针指向此对象。通过工厂获取此虚基类指针,通过运行时多态,调用子类实现。
注意:简单工厂 的设计是违反开放封闭原则(对扩展是开放的、对修改是封闭的)的,因为当我门新增一个产品,简单工厂类内部需要修改逻辑以生产对应的产品类。
#include <iostream>
using namespace std;
//操作类型
typedef enum OPERATIONTYPE
{
OPERATION_ADD,
OPERATION_SUB,
}OPERATIONTYPE;
//运算符类
class operation
{
public:
int a;
int b;
public:
operation(/* args */){}
virtual ~operation(){}
public:
virtual int getResult() = 0;
};
//加法类
class operationAdd : public operation
{
public:
operationAdd(/* args */){}
~operationAdd(){}
public:
int getResult()
{
return a+b;
}
};
//减法类
class operationSub : public operation
{
public:
operationSub(/* args */){}
~operationSub(){}
public:
int getResult()
{
return a-b;
}
};
//简单工厂类
class operationFactory
{
public:
operationFactory(/* args */){}
~operationFactory(){}
public:
operation* createOperation(OPERATIONTYPE type)
{
operation *oper;
switch (type)
{
case OPERATION_ADD:
oper = new operationAdd();
break;
case OPERATION_SUB:
oper = new operationSub();
break;
default:
oper = nullptr;
break;
}
return oper;
}
};
//客户端
int main()
{
//创建工厂
operationFactory opeFactory;
//通过工厂生产操作类
operation *opeAdd = opeFactory.createOperation(OPERATION_ADD);
operation *opeSub = opeFactory.createOperation(OPERATION_SUB);
opeAdd->a = 1;
opeAdd->b = 2;
cout<<"add result: "<<opeAdd->getResult()<<endl;
opeSub->a = 3;
opeSub->b = 4;
cout<<"sub result:" <<opeSub->getResult()<<endl;
//销毁对象
delete opeAdd;
opeAdd = nullptr;
delete opeSub;
opeSub = nullptr;
return 0;
}
工厂模式:
多个工厂,多个产品,每个产品对应于一个工厂。此时工厂和产品都是通过虚基类的方式构建。当增加一个新产品时,同时增加一个新工厂。增加新工厂属于扩展,不会修改以前工厂类和产品类的任何代码。可以看过多个独立的简单工厂模式构成了工厂方法模式,工厂模式是在开放封闭原则的基础上针对简单工厂的升级。
#include <iostream>
using namespace std;
//运算符类
class operation
{
public:
int a;
int b;
public:
operation(/* args */){}
virtual ~operation(){}
public:
virtual int getResult() = 0;
};
//加法类
class operationAdd : public operation
{
public:
operationAdd(/* args */){}
~operationAdd(){}
public:
int getResult()
{
return a+b;
}
};
//减法类
class operationSub : public operation
{
public:
operationSub(/* args */){}
~operationSub(){}
public:
int getResult()
{
return a-b;
}
};
//工厂基类
class factory
{
public:
factory(/* args */){}
virtual ~factory(){}
public:
virtual operation *createOperation() = 0;
};
//加法工厂类
class addFactory : public factory
{
public:
addFactory(/* args */){}
~addFactory(){}
public:
operation *createOperation()
{
return new operationAdd();
}
};
//减法工厂类
class subFactory : public factory
{
public:
subFactory(/* args */){}
~subFactory(){}
public:
operation *createOperation()
{
return new operationSub();
}
};
//客户端
int main()
{
//创建运算符类工厂
factory *addFac = new addFactory();
factory *subFac = new subFactory();
//通过运算符类工厂生产对应运算符类
operation *addOpe = addFac->createOperation();
operation *subOpe = subFac->createOperation();
//使用操作类
addOpe->a = 1;
addOpe->b = 2;
subOpe->a = 3;
subOpe->b = 4;
cout<< "add result: " << addOpe->getResult() <<endl;
cout<< "sub result: " << subOpe->getResult() <<endl;
//销毁对象
delete addOpe;
addOpe = nullptr;
delete subOpe;
subOpe = nullptr;
delete addFac;
addFac = nullptr;
delete subFac;
subFac = nullptr;
return 0;
}