Design Pattern之策略模式

策略模式(Strategy):它定義了算法家族,分別封裝起來,讓它們之間可以互相替換,此模式讓算法的變化,不會影響到使用算法的客戶。
策略模式就是用來封裝算法的,實踐中,我們發現可以用它來封裝幾乎任何類型的規則,只要在分析過程中聽到需要在不同時間應用不同的業務規則,就可以考慮使用策略模式處理這種變化的可能性[DPE]。下面將實現一個策略模式是商場的收銀系統的demo,參考大話設計模式,利用c++對代碼進行了改編。該收銀系統有很多計算方式,這裏只列舉了正常的計算方式、滿一定金額返現以及打折這三種方式。這三種算法可以封裝成單獨的三個類並分別繼承一個抽象的算法類,再從一個類裏對該三種算法進行處理。貼出代碼:

//算法的父類
#ifndef  _CASH_H
#define _CASH_H

class Cash
{
public:
    virtual ~Cash()
    {

    }
    virtual double PayCash(double __money) = 0;
};
#endif // ! _CASH_H
//正常計算方式
#ifndef _NORMAL_H
#define _NORMAL_H
#include "cash.h"

class PayNormal : public Cash
{
public:
    PayNormal() {}
    ~PayNormal() {}
    virtual double PayCash( double __money )
    {
        return __money;
    }
};
#endif
//打折計算方式
#ifndef _REBATE_H
#define _REBATE_H
#include "cash.h"

class PayRebate : public Cash
{
public:
    PayRebate(double __rebate)
    {
        m_rebate = __rebate;
    }

    ~PayRebate() {}
    virtual double PayCash(double __money )
    {
        return (__money*m_rebate);
    }
private:
    double m_rebate;
};
#endif
//滿減計算方式
#ifndef _RETURN_H
#define _RETURN_H
#include "cash.h"

class PayReturn : public Cash
{
public:
    PayReturn(double __condition, double __return)
    {
        m_condition = __condition;
        m_return = __return;
    }

    ~PayReturn() {}

    virtual double PayCash(double __money)
    {
        double ret = __money;
        if (__money >= m_condition)
        {
            ret = __money - (__money / m_condition)*m_return;
        }
        return ret;
    }
private:
    double m_condition;
    double m_return;
};
#endif
//對三種計算方式進行了封裝,在主函數裏看不到這三個算法類
#ifndef  _CONTEXT_H
#define  _CONTEXT_H
#include "cash.h"
#include "normal.h"
#include "rebate.h"
#include "returncash.h"
#define  NORMAL   1
#define  RETURN   2
#define  REBATE   3
class Context
{
public:
    Context( int __paymethod )
    {
        m_pCash = NULL;
        switch (__paymethod)
        {
        case NORMAL:
        {
            m_pCash = new PayNormal();
        }
            break;
        case RETURN:
        {
            m_pCash = new PayReturn( 300,100);
        }
            break;
        case REBATE:
        {
            m_pCash = new PayRebate(0.8);
        }
            break;
        default:
            break;
        }
    }
    ~Context() 
    { 
        delete m_pCash; 
        m_pCash = NULL;
    }
    double GetReult( double __money )
    {
        double ret = m_pCash->PayCash(__money);
        return ret;
    }
private:
    Cash *m_pCash;
};
#endif // ! _CONTEXT_H
//run.cpp 主函數裏只需要把算法方式給Context類,並把金額給Context類即可
#include <tchar.h>
#include "context.h"
int _tmain(int argc, TCHAR* argv[])
{
    Context *text = new Context(NORMAL);
    double result = text->GetReult(300);
    _tprintf(_T("NORMAL::result == %f\n"), result);
    delete text;
    text = NULL;

    text = new Context(RETURN);
    result = text->GetReult(300);
    _tprintf(_T("RETURN::result == %f\n"), result);
    delete text;
    text = NULL;

    text = new Context(REBATE);
    result = text->GetReult(300);
    _tprintf(_T("RETURN::result == %f\n"), result);
    delete text;
    text = NULL;
}

該程序的UML圖如下:
這裏寫圖片描述
運行結果如下:
這裏寫圖片描述

發佈了65 篇原創文章 · 獲贊 9 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章