设计模式 -- 简单工厂模式 -- c++实现

注:本文主要代码基于大话设计模式里的C#代码(第1章)。


简单工厂模式只要用一个简单的switch case语句就可以实现了。

在c++下,可以使用函数实现工厂函数。


下面代码是为了实现将运算操作逻辑解耦出来。以便复用。同时采用工厂模式生产具体的运算类。

将运算操作进行抽象是为了以后可以方便增加其他的运算操作。


代码如下:

/*****************************************************
* 模式名:简单工厂模式
* 时间:2010.6.3
*                                       -by gouki04
*****************************************************/

#pragma once

#include <cstdio>

//////////////////////////////////////////////////////////////////////////
class cOperation;    // 运算符基类
class cOperationAdd; // 加法类
class cOperationSub; // 减法类
class cOperationMul; // 乘法类
class cOperationDiv; // 除法类
cOperation* CreateOperation(const char operate); // 简单工厂方法
//////////////////////////////////////////////////////////////////////////

// 运算符基类
class cOperation
{
private:
    double m_numberA;
    double m_numberB;

protected:
    double m_result;

public:
    cOperation(double numA = 0, double numB = 0) : m_numberA(numA), m_numberB(numB), m_result(0) {}
    virtual ~cOperation() {}

public:
    double GetNumberA() const { return m_numberA; }
    double GetNumberB() const { return m_numberB; }
    void SetNumberA(double number) { m_numberA = number; }
    void SetNumberB(double number) { m_numberB = number; }

    virtual double GetResult() const { return m_result; };
    virtual void Compute() = 0;
};

// 加法类
class cOperationAdd : public cOperation
{
public:
    cOperationAdd(double numA = 0, double numB = 0) : cOperation(numA, numB) {}
    ~cOperationAdd() {}
    void Compute() { m_result = GetNumberA() + GetNumberB(); }
};

// 减法类
class cOperationSub : public cOperation
{
public:
    cOperationSub(double numA = 0, double numB = 0) : cOperation(numA, numB) {}
    ~cOperationSub() {}
    void Compute() { m_result = GetNumberA() - GetNumberB(); }
};

// 乘法类
class cOperationMul : public cOperation
{
public:
    cOperationMul(double numA = 0, double numB = 0) : cOperation(numA, numB) {}
    ~cOperationMul() {}
    void Compute() { m_result = GetNumberA() * GetNumberB(); }
};

// 除法类
class cOperationDiv : public cOperation
{
public:
    cOperationDiv(double numA = 0, double numB = 0) : cOperation(numA, numB) {}
    ~cOperationDiv() {}
    void Compute()
    {
        if (GetNumberB() == 0)
            throw "除数不能为0";

        m_result = GetNumberA() / GetNumberB();
    }
};

// 简单工厂方法
cOperation* CreateOperation(const char operate)
{
    cOperation* pOper = NULL;
    switch (operate) {
    case '+':
        pOper = new cOperationAdd();
        break;
    case '-':
        pOper = new cOperationSub();
        break;
    case '*':
        pOper = new cOperationMul();
        break;
    case '/':
        pOper = new cOperationDiv();
        break;
    default:
        break;
    }
    return pOper;
}

// 测试的main函数
//int main()
//{
//    using namespace std;
//
//    try {
//        cOperation* oper = CreateOperation('/');
//        oper->SetNumberA(1);
//        oper->SetNumberB(0);
//        oper->Compute();
//        cout << oper->GetResult() << endl;
//    }
//    catch (const char* msg) {
//        cout << msg << endl;
//    }
//
//    return 0;
//}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章