設計模式 -- 簡單工廠模式 -- 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;
//}

 

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