/************************************************************************/
/* 运算符工厂类 */
/************************************************************************/
#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_
#include "stdafx.h"
#include "COperation.h"
COperation::COperation()
: _dNumA(0)
, _dNumB(0)
{
}
/************************************************************************/
/* 运算符基类 */
/************************************************************************/
#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_
/************************************************************************/
/* 加法运算符子类,继承于运算符基类 */
/************************************************************************/
#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_
/************************************************************************/
/* 除法运算符子类,继承于运算符基类 */
/************************************************************************/
#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_
/************************************************************************/
/* 乘法运算符子类,继承于运算符基类 */
/************************************************************************/
#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_
/************************************************************************/
/* 减法运算符子类,继承于运算符基类 */
/************************************************************************/
#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_
// 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;
}
/************************************************************************/
/* 抽象水果类(abstract Product) */
/************************************************************************/
#ifndef _IFRUIT_H_
#define _IFRUIT_H_
#include <string>
#include <iostream>
using namespace std;
class IFruit
{
public:
virtual void grow() = 0;
virtual void harvest() = 0;
virtual void plant() = 0;
};
#endif _IFRUIT_H_
/************************************************************************/
/* 抽象水果园丁类(abstract Factory) */
/************************************************************************/
#ifndef _IFRUIT_GARDENER_H_
#define _IFRUIT_GARDENER_H_
#include "IFruit.h"
class IFruitGardener
{
public:
virtual IFruit* Factory() = 0;
};
#endif _IFRUIT_GARDENER_H_
/************************************************************************/
/* 具体的苹果类(Concrete Product) */
/************************************************************************/
#ifndef _APPLE_H_
#define _APPLE_H_
#include "IFruit.h"
class CApple : public IFruit
{
public:
void grow()
{
cout << "Apple is growing..." << endl;
};
void harvest()
{
cout << "Apple has been harvested." << endl;
};
void plant()
{
cout << "Apple has been planted." << endl;
};
int GetTreeAge()
{
return m_iAppleTreeAge;
};
void SetTreeAge(const int iAge)
{
m_iAppleTreeAge = iAge;
}
private:
int m_iAppleTreeAge;
};
#endif _APPLE_H_
/************************************************************************/
/* 具体的葡萄类(Concrete Product) */
/************************************************************************/
#ifndef _GRAPE_H_
#define _GRAPE_H_
#include "IFruit.h"
class CGrape : public IFruit
{
public:
void grow()
{
cout << "Grape is growing..." << endl;
};
void harvest()
{
cout << "Grape has been harvested." << endl;
};
void plant()
{
cout << "Grape has been planted." << endl;
};
bool GetSeedless()
{
return m_bSeedless;
};
void SetSeedless(const bool bSeedless)
{
m_bSeedless = bSeedless;
};
private:
bool m_bSeedless;
};
#endif _GRAPE_H_
/************************************************************************/
/* 具体的草莓类(Concrete Product) */
/************************************************************************/
#ifndef _STRAWBERRY_H_
#define _STRAWBERRY_H_
#include "IFruit.h"
class CStrawberry : public IFruit
{
public:
void grow()
{
cout << "Strawberry is growing..." << endl;
};
void harvest()
{
cout << "Strawberry has been harvested." << endl;
};
void plant()
{
cout << "Strawberry has been planted." << endl;
};
};
#endif _STRAWBERRY_H_
/************************************************************************/
/* 具体的苹果园丁类(Concrete Factory) */
/************************************************************************/
#ifndef _APPLE_GARDENER_H_
#define _APPLE_GARDENER_H_
#include "IFruitGardener.h"
#include "CApple.h"
class CAppleGardener : public IFruitGardener
{
public:
CAppleGardener():m_pApple(NULL){};
IFruit* Factory()
{
if (NULL == m_pApple)
{
m_pApple = new CApple();
}
return m_pApple;
};
private:
CApple* m_pApple;
};
#endif _APPLE_GARDENER_H_
/************************************************************************/
/* 具体的葡萄园丁类(Concrete Factory) */
/************************************************************************/
#ifndef _GRAPE_GARDENER_H_
#define _GRAPE_GARDENER_H_
#include "IFruitGardener.h"
#include "CGrape.h"
class CGrapeGardener : public IFruitGardener
{
public:
CGrapeGardener():m_pGrape(NULL){};
IFruit* Factory()
{
if (NULL == m_pGrape)
{
m_pGrape = new CGrape();
}
return m_pGrape;
};
private:
CGrape* m_pGrape;
};
#endif _GRAPE_GARDENER_H_
/************************************************************************/
/* 具体的草莓园丁类(Concrete Factory) */
/************************************************************************/
#ifndef _STRAWBERRY_GARDENER_H_
#define _STRAWBERRY_GARDENER_H_
#include "IFruitGardener.h"
#include "CStrawberry.h"
class CStrawberryGardener : public IFruitGardener
{
public:
CStrawberryGardener():m_pStrawberry(NULL){};
IFruit* Factory()
{
if (NULL == m_pStrawberry)
{
m_pStrawberry = new CStrawberry();
}
return m_pStrawberry;
};
private:
CStrawberry* m_pStrawberry;
};
#endif _STRAWBERRY_GARDENER_H_
// FactoryMethodApplication.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#include "IFruitGardener.h"
#include "CAppleGardener.h"
#include "CGrapeGardener.h"
#include "CStrawberryGardener.h"
int _tmain(int argc, _TCHAR* argv[])
{
static IFruitGardener* pFruitFactory1 = NULL;
static IFruitGardener* pFruitFactory2 = NULL;
static IFruit* pFruit1 = NULL;
static IFruit* pFruit2 = NULL;
pFruitFactory1 = new CAppleGardener();
if (NULL != pFruitFactory1)
{
pFruit1 = pFruitFactory1->Factory();
if (NULL != pFruit1)
{
pFruit1->grow();
pFruit1->harvest();
pFruit1->plant();
}
}
pFruitFactory2 = new CGrapeGardener();
if (NULL != pFruitFactory2)
{
pFruit2 = pFruitFactory2->Factory();
if (NULL != pFruit2)
{
pFruit2->grow();
pFruit2->harvest();
pFruit2->plant();
}
}
Sleep(10000);
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有