码迷,mamicode.com
首页 > 其他好文 > 详细

【设计模式】简单工厂模式

时间:2014-07-13 18:01:20      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   

以面向对象的思想和简单工厂模式,写一个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;
} 


运行结果:
bubuko.com,布布扣

基类Operation将运算的两个操作数进行封装,使用户无法直接访问操作数。如果用户要访问或修改操作数,在基类中添加函数接口即可。同时运用继承,将具体的操作符从基类派生出来,并根据运算符特性重写基类中的纯虚函数。这样做的好处是能够降低耦合度。假设需要向代码中添加新的运算符,那么只需要将新的运算符继承自Operation基类即可,不需要修改其它派生类中的代码。本例使用一个工厂函数FactoryFunction来实例化对象。该工厂函数返回一个基类指针,指向派生类对象,使得用户可以调用相同的函数生成不同的对象。然后根据多态性质调用派生类的Calculate虚函数。

参考:
《大话设计模式》第1章。

【设计模式】简单工厂模式,布布扣,bubuko.com

【设计模式】简单工厂模式

标签:style   blog   http   color   使用   os   

原文地址:http://blog.csdn.net/nestler/article/details/37738095

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!