Design Pattern之简单工厂模式

简单工厂模式顾名思义,就是专门负责生产的,我们需要什么,就让其生产什么。该模式在开发过程中经常会用到,其好处就是在于设计的类能够达到可维护、可复用、可扩展、灵活性好的目的。
例如,利用C++设计一个加减乘除的小程序,直观的思维直接通过输入判断符号类型(+、-、*、/),然后根据不同的类型,直接算出结果,这样有一个问题,如果以后要修改的时候,需要在同一个文件里找到需要增加判断的地方,容易出错。因此我们可以设计一个抽象类,表示运算符,加减乘除可以视其为四个类,分别继承于抽象类,创建一个运算符工厂类,生成我们需要的类对象,通过多态动态绑定需要调用的类成员函数。
下面是该程序的UML图:
这里写图片描述
通过UML图可以很清楚地看到每个类之间的关系。下面请看程序代码:

//operation.h
#ifndef _OPERATION_H
#define _OPERATION_H

class Operation
{
public:
    virtual ~Operation() {}
    virtual double GetResult() = 0;
    void   SetValue(double numberBefore, double numberAfter)
    {
        m_numberBefore = numberBefore;
        m_numberAfter  = numberAfter;
    }
protected:
    double m_numberBefore;
    double m_numberAfter;
};
#endif

Operation是一个抽象类,拥有一个纯虚函数,一个普通成员函数,两个参与运算的double型的成员变量。

#ifndef _OPERATIONADD_H
#define _OPERATIONADD_H
#include "operation.h"

class OperationAdd : public  Operation
{
public:
    OperationAdd() {}
    ~OperationAdd() {}
    virtual double GetResult()
    {
        return (m_numberBefore + m_numberAfter);
    }
};
#endif
#ifndef _OPERATIONSUB_H
#define _OPERATIONSUB_H
#include "operation.h"

class OperationSub : public  Operation
{
public:
    OperationSub() {}
    ~OperationSub() {}
    virtual double GetResult()
    {
        return (m_numberBefore - m_numberAfter);
    }
};
#endif
#ifndef _OPERATIONMUL_H
#define _OPERATIONMUL_H
#include "operation.h"

class OperationMul : public  Operation
{
public:
    OperationMul() {}
    ~OperationMul() {}
    virtual double GetResult()
    {
        return (m_numberBefore * m_numberAfter);
    }
};
#endif
#ifndef _OPERATIONDIV_H
#define _OPERATIONDIV_H
#include "operation.h"
#include "stdio.h"

class OperationDiv : public  Operation
{
public:
    OperationDiv() {}
    ~OperationDiv() {}
    virtual double GetResult()
    {
        if (m_numberAfter == 0)
        {
            printf("除数不能为0,请重新输入\n");
            return -1;
        }
        return (m_numberBefore / m_numberAfter);
    }
};
#endif

以上四个类分别继承Operation抽象类,并实现GetResult函数,因此,通过父类指针指向子类对象,可实现动态绑定。

//operationfactory.h
#ifndef  _OPERATIONFACTORY_H
#define  _OPERATIONFACTORY_H
#include "operation.h"
#define  ADD   0x01
#define  SUB   0x02
#define  MUL   0x03
#define  DIV   0x04
class OperationFactory
{
public:
    static Operation* createOperate(int operateSeq);
};
#endif // ! _OPERATIONFACTORY_H

//operationfactory.cpp
#include "operationfactory.h"
#include "string.h"
#include "operationadd.h"
#include "operationsub.h"
#include "operationmul.h"
#include "operationdiv.h"

Operation* OperationFactory::createOperate(int operateSeq)
{
    Operation* oper = NULL;
    switch (operateSeq)
    {
    case ADD:
        oper = new OperationAdd();
        break;
    case SUB:
        oper = new OperationSub();
        break;
    case MUL:
        oper = new OperationMul();
        break;
    case DIV:
        oper = new OperationDiv();
        break;
    default:
        break;
    }
    return oper;
}

工厂类中的静态成员函数根据函数参数为我们生产对应的计算对象,获取对象之后,通过SetValue函数设置参与计算的数值即可。下面是main函数的调用实现:

//run.cpp
#include "operationfactory.h"
#include <tchar.h>
#include <stdio.h>

int _tmain(int argc, TCHAR* argv[])
{
    Operation *oper = NULL;
    oper = OperationFactory::createOperate(ADD);
    oper->SetValue(3, 97);
    double result = oper->GetResult();
    printf("add:reulst=%f\n", result);
    delete oper;
    oper = NULL;

    oper = OperationFactory::createOperate(SUB);
    oper->SetValue(9,4);
    result = oper->GetResult();
    printf("sub:reulst=%f\n", result);
    delete oper;
    oper = NULL;

    oper = OperationFactory::createOperate(MUL);
    oper->SetValue(9, 4);
    result = oper->GetResult();
    printf("mul:reulst=%f\n", result);
    delete oper;
    oper = NULL;

    oper = OperationFactory::createOperate(DIV);
    oper->SetValue(9, 4);
    result = oper->GetResult();
    printf("mul:reulst=%f\n", result);
    delete oper;
    oper = NULL;

    return 0;
}

运行结果如下:
这里写图片描述
通过代码可以很清楚的理解了简单工厂模式是怎么设计的,从中可以看出如果增加一个新的计算模式,只需要增加一个新类继承抽象类,而不用关心其他计算方式的实现逻辑。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值