简单工厂模式实例
题目:实现计算器的输入2个数和运算符,得到结果
工程结构:
(1)头文件
COperationFactory.h(运算符工厂类)
(2)源文件
SimpleFactory.cpp(客户端应用类,主函数所在)
(3)运算类
COperation.cpp(运算符基类)
COperation.h
COperationAdd.h(加法运算符子类,继承于COperation)
COperationDiv.h (除法运算符子类,继承于COperation)
COperationMul.h (乘法运算符子类,继承于COperation)
COperationSub.h(减法运算符子类,继承于COperation)
============= 代码实现部分 =============
COperationFactory.h(运算符工厂类)
/************************************************************************/
/* 运算符工厂类 */
/************************************************************************/
#ifndef _OPERATION_FACTORY_H_
#define _OPERATION_FACTORY_H_
#include "stdafx.h"
#include "COperation.h"
#include "COperationAdd.h"
#include "COperationSub.h"
#include "COperationMul.h"
#include "COperationDiv.h"
#include "COperationFactory.h"
class COperationFactory
{
public:
COperationFactory(){};
~COperationFactory(){};
// 根据入参的不同,创建其对应的运算符类指针。就像是个工厂,创建用户指定的运算符类指针
static COperation* NewOperation(const string& strOperate)
{
// 入参合法性判断,防止后面的strOperate[0]发生越界访问
if (strOperate.size() != 1)
{
return NULL;
}
COperation* pOperation = NULL;
switch (strOperate[0])
{
case '+':
pOperation = new COperationAdd();
break;
case '-':
pOperation = new COperationSub();
break;
case '*':
pOperation = new COperationMul();
break;
case '/':
pOperation = new COperationDiv();
break;
default:
break;
}
return pOperation;
};
};
#endif _OPERATION_FACTORY_H_
</div>
COperation.cpp(运算符基类)
#include "stdafx.h"
#include "COperation.h"
COperation::COperation()
: _dNumA(0)
, _dNumB(0)
{
}
</div>
COperation.h
/************************************************************************/
/* 运算符基类 */
/************************************************************************/
#ifndef _COPERATION_H_
#define _COPERATION_H_
class COperation
{
public:
COperation();
~COperation(){};
// 设置被运算数
void SetNumA(double dNumA)
{
_dNumA = dNumA;
};
// 获取被运算数
double GetNumA()
{
return _dNumA;
};
// 设置运算数
void SetNumB(double dNumB)
{
_dNumB = dNumB;
};
// 获取运算数
double GetNumB()
{
return _dNumB;
};
// 计算结果且在子类中实现各自的运算方法结果
virtual double Result()
{
double dResult = 0;
return dResult;
}
private:
double _dNumA;
double _dNumB;
};
#endif _COPERATION_H_
</div>
COperationAdd.h(加法运算符子类,继承于COperation)
/************************************************************************/
/* 加法运算符子类,继承于运算符基类 */
/************************************************************************/
#ifndef _COPERATION_ADD_H_
#define _COPERATION_ADD_H_
#include "COperation.h"
class COperationAdd : public COperation
{
public:
COperationAdd(){};
~COperationAdd(){};
double Result()
{
return (GetNumA() + GetNumB());
};
};
#endif _COPERATION_ADD_H_
</div>
COperationDiv.h (除法运算符子类,继承于COperation)
/************************************************************************/
/* 除法运算符子类,继承于运算符基类 */
/************************************************************************/
#ifndef _COPERATION_DIV_H_
#define _COPERATION_DIV_H_
#include "COperation.h"
class COperationDiv : public COperation
{
public:
COperationDiv(){};
~COperationDiv(){};
double Result()
{
double dResult = 0;
if (0 != GetNumB())
{
dResult = (GetNumA() / GetNumB());
}
else
{
cout << "error: divisor is ";
}
return dResult;
};
};
#endif _COPERATION_DIV_H_
</div>
COperationMul.h (乘法运算符子类,继承于COperation)
/************************************************************************/
/* 乘法运算符子类,继承于运算符基类 */
/************************************************************************/
#ifndef _COPERATION_MUL_H_
#define _COPERATION_MUL_H_
#include "COperation.h"
class COperationMul : public COperation
{
public:
COperationMul(){};
~COperationMul(){};
double Result()
{
return (GetNumA() * GetNumB());
};
};
#endif _COPERATION_MUL_H_
</div>
COperationSub.h(减法运算符子类,继承于COperation)
/************************************************************************/
/* 减法运算符子类,继承于运算符基类 */
/************************************************************************/
#ifndef _COPERATION_SUB_H_
#define _COPERATION_SUB_H_
#include "COperation.h"
class COperationSub : public COperation
{
public:
COperationSub(){};
~COperationSub(){};
double Result()
{
return (GetNumA() - GetNumB());
};
};
#endif _COPERATION_SUB_H_
</div>
SimpleFactory.cpp(客户端应用类,主函数所在)
// SimpleFactory.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "COperationFactory.h"
int _tmain(int argc, _TCHAR* argv[])
{
// 通过运算符工厂创建加法运算
COperation* OperAdd = COperationFactory::NewOperation("+");
if (NULL != OperAdd)
{
OperAdd->SetNumA(168); // 设置被加数
OperAdd->SetNumB(105); // 设置加数
cout << "168 + 105 = " << (OperAdd->Result()) << endl;
}
// 通过运算符工厂创建减法运算
COperation* OperSub = COperationFactory::NewOperation("-");
if (NULL != OperSub)
{
OperSub->SetNumA(168); // 设置被减数
OperSub->SetNumB(105); // 设置减数
cout << "168 - 105 = " << (OperSub->Result()) << endl;
}
// 通过运算符工厂创建乘法运算
COperation* OperMul = COperationFactory::NewOperation("*");
if (NULL != OperMul)
{
OperMul->SetNumA(168); // 设置被乘数
OperMul->SetNumB(105); // 设置乘数
cout << "168 * 105 = " << (OperMul->Result()) << endl;
}
// 通过运算符工厂创建除法运算
COperation* OperDiv = COperationFactory::NewOperation("/");
if (NULL != OperDiv)
{
OperDiv->SetNumA(168); // 设置被除数
OperDiv->SetNumB(105); // 设置除数
cout << "168 / 105 = " << (OperDiv->Result()) << endl;
OperDiv->SetNumB(0); // 改变除数
cout << (OperDiv->Result()) << endl;
}
// 阻止控制台进程结束,便于查看结果
int nEnd = 0;
cin >> nEnd;
return 0;
}
</div>
抽象工厂模式实例
工程结构:
(1)抽象产品类
IFruit.h
(2)抽象工厂类
IFruitGardener.h
(3)具体产品类
CApple.h
CGrape.h

