主函数之内也即客户端界面程序(此处未实现界面)只使用了context一个类实现了封装
#include "operationFactory.h"
int main(int argc, char *argv[])
{
//主函数之内也即客户端界面程序(此处未实现界面)只使用了context一个类实现了封装
OperationFactoryContext *context = new OperationFactoryContext();
if (context->setInfo(10, '+', 20))
cout << 10 << '+' << 20 << "=" << context->contextGetResult() << endl;
delete context;
return 0;
}
基类:Operation 使用虚析构函数否则delete时不会释放派生类的资源,会报错
派生类:OperationAdd OperationSub OperationMul OperationDiv 使用虚方法,实现简单工厂模式
策略类:OperationFactoryContext 外部只操作这个类
#ifndef OPERATIONFACTORY
#define OPERATIONFACTORY
#include <iostream>
using namespace std;
class Operation
{
public:
Operation():numA(0),numB(0){cout << "Operation" << endl;}
virtual ~Operation(){cout << "~Operation" << endl;}
double getnumA(){return numA;}
double getnumB(){return numB;}
void setnumA(int A){numA = A;}
void setnumB(int B){numB = B;}
double virtual getResult() = 0;
protected:
double numA;
double numB;
};
class OperationAdd : public Operation
{
public:
OperationAdd(){cout << "OperationAdd" << endl;}
~OperationAdd(){cout << "~OperationAdd" << endl;}
double getResult(){return (double)(numA+numB);}
};
class OperationSub : public Operation
{
public:
OperationSub(){cout << "OperationSub" << endl;}
~OperationSub(){cout << "~OperationSub" << endl;}
double getResult(){return double(numA-numB);}
};
class OperationMul : public Operation
{
public:
OperationMul(){cout << "OperationMul" << endl;}
~OperationMul(){cout << "~OperationMul" << endl;}
double getResult(){return (double)(numA*numB);}
};
class OperationDiv : public Operation
{
public:
OperationDiv(){cout << "OperationDiv" << endl;}
~OperationDiv(){cout << "~OperationDiv" << endl;}
double getResult(){return (double)(numA/numB);}
};
class OperationFactoryContext
{
public:
OperationFactoryContext(){oper = NULL;}
/* 动态联编时delete基类的指针,需要基类有虚析构函数,不然会调到基类的析构函数而不调用派生类的 */
~OperationFactoryContext(){delete oper;}
bool setInfo(double A, char operate, double B)
{
cout << "operate=" << operate << endl;
switch(operate)
{
case '+':
this->oper = new OperationAdd();break;
case '-':
this->oper = new OperationSub();break;
case '*':
this->oper = new OperationMul();break;
case '/':
this->oper = new OperationDiv();break;
default:
cout << "input operate " << operate << " error" << endl;
return false;
}
oper->setnumA(A);
oper->setnumB(B);
return true;
}
double contextGetResult(){return oper->getResult();}
private:
Operation *oper;
};
#endif