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

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

时间:2014-06-21 00:29:42      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   http   color   

  简单工厂模式没有抽象类,只有一个具体工厂类,所有产品的生产都由这个工厂类的对象来负责,如果这个工厂类中生产产品的方法被声明为静态的,那么连这个工厂对象也不是必须的了,直接使用工厂类名就可以调用生产方法。生产方法被声明为静态的,所以简单工厂模式也叫静态工厂模式。简单工厂模式并不是一个好的设计模式,因为它不符合开闭原则。

bubuko.com,布布扣

 

#include <iostream>
using namespace std;
class Operation
{
private:
    double _numberA;
    double _numberB;
public:
    Operation(){}
    Operation(double numA,double numB):_numberA(numA),_numberB(numB){}
    void set_numberA(double numA)
    {
        _numberA=numA;
    }
    double get_numberA()
    {
        return _numberA;
    }
    void set_numberB(double numB)
    {
        _numberB=numB;
    }
    double get_numberB()
    {
        return _numberB;
    }
    virtual double GetResult()
    {
        double result=0;
        return result;
    }
};
//加减乘除类
class OperationAdd:public Operation
{
public:
    OperationAdd(){}
    OperationAdd(double numA,double numB):Operation(numA,numB){}
    double GetResult()
    {
        double result=0;
        result=get_numberA()+get_numberB();
        return result;
    }
};
class OperationSub:public Operation
{
public:
    OperationSub(){}
    OperationSub(double numA,double numB):Operation(numA,numB){}
    double GetResult()
    {
        double result=0;
        result=get_numberA()-get_numberB();
        return result;
    }
};
class OperationMul:public Operation
{
public:
    OperationMul(){}
    OperationMul(double numA,double numB):Operation(numA,numB){}
    double GetResult()
    {
        double result=0;
        result=get_numberA()*get_numberB();
        return result;
    }
};
class OperationDiv:public Operation
{
public:
    OperationDiv(){}
    OperationDiv(double numA,double numB):Operation(numA,numB){}
    double GetResult()
    {
        double result=0;
        if(get_numberB()==0)
        {
            cout << "除数不能为0" << endl;
            return -1;
        }
        result=get_numberA()/get_numberB();
        return result;
    }
};
//简单运算工厂类
class OperationFactory
{
public:
    Operation *CreateOperate(char operate)
    {
        Operation *oper=NULL;
        switch(operate)
        {
        case +:
            oper=new OperationAdd();
            break;
        case -:
            oper=new OperationSub();
            break;
        case *:
            oper=new OperationMul();
            break;
        case /:
            oper=new OperationDiv();
            break;
        default:
            break;
        }
        return oper;
    }
};
int main()
{
    Operation *oper;
    OperationFactory operFactory;
    oper=operFactory.CreateOperate(+);
    oper->set_numberA(1);
    oper->set_numberB(2);
    double result=oper->GetResult();
    cout << "result=" << result << endl;
    return 0;
}

 

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

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

标签:style   class   blog   code   http   color   

原文地址:http://www.cnblogs.com/awy-blog/p/3795592.html

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