简单工厂+策略模式实现简易计算器

主函数之内也即客户端界面程序(此处未实现界面)只使用了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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值