简单工厂模式解释:
简单工厂模式(Simple Factory Pattern)属于类的创新型模式,又叫静态工厂方法模式(Static FactoryMethod Pattern),是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。
简单工厂模式的UML图:
简单工厂模式中包含的角色及其相应的职责如下:
工厂角色(Creator):这是简单工厂模式的核心,由它负责创建所有的类的内部逻辑。当然工厂类必须能够被外界调用,创建所需要的产品对象。
抽象(Product)产品角色:简单工厂模式所创建的所有对象的父类,注意,这里的父类可以是接口也可以是抽象类,它负责描述所有实例所共有的公共接口。
具体产品(Concrete Product)角色:简单工厂所创建的具体实例对象,这些具体的产品往往都拥有共同的父类。
简单工厂模式深入分析:
简单工厂模式解决的问题是如何去实例化一个合适的对象。
简单工厂模式的核心思想就是:有一个专门的类来负责创建实例的过程。
具体来说,把产品看着是一系列的类的集合,这些类是由某个抽象类或者接口派生出来的一个对象树。而工厂类用来产生一个合适的对象来满足客户的要求。
如果简单工厂模式所涉及到的具体产品之间没有共同的逻辑,那么我们就可以使用接口来扮演抽象产品的角色;如果具体产品之间有功能的逻辑或,我们就必须把这些共同的东西提取出来,放在一个抽象类中,然后让具体产品继承抽象类。为实现更好复用的目的,共同的东西总是应该抽象出来的。
在实际的的使用中,抽闲产品和具体产品之间往往是多层次的产品结构,如下图所示:
下面以运算器的设计例子,该例子用的模板实现:
抽象产品:运算类
#ifndef OPERATION_H
#define OPERATION_H
template <class T>
class Operation
{
public:
virtual T computeOpreration() = 0;
void setNumA(T _numA);
void setNumB(T _numB);
T getNumA();
T getNumB();
private:
T numA;
T numB;
};
template<class T>
void Operation<T>::setNumA(T _numA)
{
numA = _numA;
}
template<class T>
void Operation<T>::setNumB(T _numB)
{
numB = _numB;
}
template<class T>
T Operation<T>::getNumA()
{
return numA;
}
template<class T>
T Operation<T>::getNumB()
{
return numB;
}
#endif
具体产品:加法类
#include"Operation.h"
#ifndef OPERATIONADD_H
#define OPERATIONADD_H
template<class T>
class OperationAdd : public Operation<T>
{
public:
T computeOpreration();
};
template<class T>
T OperationAdd<T>::computeOpreration()
{
return (getNumA() + getNumB());
}
#endif
具体产品:乘法类
#include"Operation.h"
#ifndef OPERATIONMUL_H
#define OPERATIONMUL_H
template<class T>
class OperationMul : public Operation<T>
{
public:
T computeOpreration();
};
template<class T>
T OperationMul<T>::computeOpreration()
{
return (getNumA() * getNumB());
}
#endif OPERATIONMUL_H
具体产品:减法类
#include"Operation.h"
#ifndef OPERATIONSUB_H
#define OPERATIONSUB_H
template<class T>
class OperationSub : public Operation<T>
{
public:
T computeOpreration();
};
template<class T>
T OperationSub<T>::computeOpreration()
{
return (getNumA() - getNumB());
}
#endif
具体产品:除法类
#include"Operation.h"
#ifndef OPERATIONDIV_H
#define OPERATIONDIV_H
template<class T>
class OperationDiv : public Operation<T>
{
public:
T computeOpreration();
};
template<class T>
T OperationDiv<T>::computeOpreration()
{
return (getNumA() / getNumB());
}
#endif
抽象工厂类:计算工厂类
#include"Operation.h"
#include"OperationSub.h"
#include"OperationAdd.h"
#include"OperationMul.h"
#include"OpertaionDiv.h"
#ifndef OPERATIONFACTORY_H
#define OPERATIONFACTORY_H
template<class T>
class OperationFactory
{
public:
Operation<T>* createOperation(char operate);
};
template<class T>
Operation<T>* OperationFactory<T>::createOperation(char operate)
{
Operation<T> *oper = NULL;
switch(operate)
{
case '+':
oper = new OperationAdd<T>();break;
case '-':
oper = new OperationSub<T>();break;
case '*':
oper = new OperationMul<T>();break;
case '/':
oper = new OperationDiv<T>();break;
}
return oper;
}
#endif
简单的main函数调用:
#include<iostream>
#include"OperationFactory.h"
using namespace std;
int main()
{
OperationFactory<double> fac;
Operation<double> *per = fac.createOperation('+');
per->setNumA(10.0);
per->setNumB(2.0);
cout<<"the result of Add is : "<<per->computeOpreration()<<endl;
return 0;
}
今天碰到主要问题还是模板的编写,以前觉得挺简单的,一直没去写,写了才发现要注意很多,尤其是编译这一块,这是今天的成果。速度跑起来。