以面向对象的思想和简单工厂模式。写一个C++计算器程序,代码例如以下:

#include <iostream>
using namespace std;
class Operation {
public:
    Operation(double left, double right)
    {
        lhs = left;
        rhs = right;
    }
    const double GetLeft() const
    {
        return lhs;
    }
    const double GetRight() const
    {
        return rhs;
    }
    void SetLeft(const double left)
    {
        lhs = left;
    }
    void SetRight(const double right)
    {
        rhs = right;
    }
    virtual double Calculate() = 0; // 纯虚函数
protected:
    double lhs;
    double rhs;
};
class Add : public Operation {
public:
    Add(double left, double right) : Operation(left, right)
    {}
    double Calculate()
    {
        return lhs + rhs;
    }
};
class Sub : public Operation {
public:
    Sub(double left, double right) : Operation(left, right)
    {}
    double Calculate()
    {
        return lhs - rhs;
    }
};
class Mul : public Operation {
public:
    Mul(double left, double right) : Operation(left, right)
    {}
    double Calculate()
    {
        return lhs * rhs;
    }
};
class Div : public Operation {
public:
    Div(double left, double right) : Operation(left, right)
    {
        try
        {
            if (right == 0)
                throw runtime_error("The divisor cannot be 0\n");
            
        }
        catch (const runtime_error &e)
        {
            cout << e.what() << endl;
            throw;
        }
    }
    double Calculate()
    {
        return lhs / rhs;
    }
};
// 工厂函数
Operation* FactoryFunction(double left, double right, char op)
{
    switch (op)
    {
    case '+':
        return new Add(left, right);
        break;
    case '-':
        return new Sub(left, right);
        break;
    case '*':
        return new Mul(left, right);
        break;
    case '/':
        return new Div(left, right);
        break;
    default:
        throw runtime_error("Operation invalid!");
        break;
    }
}
int main()
{
    Operation *add = FactoryFunction(11, 22, '+');
    Operation *sub = FactoryFunction(25, 32, '-');
    Operation *mul = FactoryFunction(11, 11, '*');
    Operation *div = FactoryFunction(50, 8, '/');
    cout << add->GetLeft() << " + " << add->GetRight() << " = " << add->Calculate() << endl;
    cout << sub->GetLeft() << " - " << sub->GetRight() << " = " << sub->Calculate() << endl;
    cout << mul->GetLeft() << " * " << mul->GetRight() << " = " << mul->Calculate() << endl;
    cout << div->GetLeft() << " / " << div->GetRight() << " = " << div->Calculate() << endl;

    div->SetLeft(40);
    cout << div->GetLeft() << " / " << div->GetRight() << " = " << div->Calculate() << endl;

    // 别忘记销毁指针
    delete add;
    delete sub;
    delete mul;
    delete div;
    system("pause");
    return 0;
} 


执行结果:


基类Operation将运算的两个操作数进行封装。使用户无法直接訪问操作数。

如果用户要訪问或改动操作数,在基类中加入函数接口就可以。同一时候运用继承,将详细的操作符从基类派生出来。并依据运算符特性重写基类中的纯虚函数。这样做的优点是可以减少耦合度。如果须要向代码中加入新的运算符。那么仅仅须要将新的运算符继承自Operation基类就可以。不须要改动其他派生类中的代码。本例使用一个工厂函数FactoryFunction来实例化对象。该工厂函数返回一个基类指针,指向派生类对象,使得用户可以调用同样的函数生成不同的对象。然后依据多态性质调用派生类的Calculate虚函数。UML图例如以下:



參考:
《大话设计模式》第1章。
posted on 2017-07-22 09:36  yutingliuyl  阅读(152)  评论(0编辑  收藏  举报