• 《大话设计模式》读书笔记(C++代码实现) 第二章:策略模式


      

      策略模式(Strategy):它定义了算法家庭,分别封装起来,让它们之间可以互相替换,此模式让算法的变化不会影响到使用算法的客户。

    1.策略模式说明:

      

    // StrategyTest01.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    //定义所有支持算法的公用接口
    //抽象算法类
    class Strategy
    {
    public :
        virtual void AlgorithmInterface() = 0;
    };
    
    class ConcreteStrategyA : public Strategy
    {
    public :
        void AlgorithmInterface()
        {
            cout<<"算法A实现"<<endl;
        }
    };
    
    class ConcreteStrategyB : public Strategy
    {
    public :
        void AlgorithmInterface()
        {
            cout<<"算法B实现"<<endl;
        }
    };
    
    class ConcreteStrategyC : public Strategy
    {
    public :
        void AlgorithmInterface()
        {
            cout<<"算法C实现"<<endl;
        }
    };
    
    
    class Context 
    {
    private :
        Strategy* strategy;
    
    public :
        //初始化时,传入具体的策略对象
        Context(Strategy* strategy)
        {
            this->strategy = strategy;
        }
        //上下文接口
        //根据具体的策略对象,调用其算法的方法
        void ContextInterface()
        {
            this->strategy->AlgorithmInterface();
        }
    };
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        ConcreteStrategyA a;
        ConcreteStrategyB b;
        ConcreteStrategyC c;
    
        Context c1(&a);
        c1.ContextInterface();
    
        Context c2(&b);
        c2.ContextInterface();
    
        Context c3(&c);
        c3.ContextInterface();
    
        system("pause");
        return 0;
    }

     2.简单工厂模式实现

    // StrategyTest02.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include <iostream>
    using namespace std;
    
    //现金收费抽象类
    class CashSuper
    {
    public:
        virtual double acceptCash(double money) = 0;
    };
    
    //正常收费类
    class CashNormal : public CashSuper
    {
    public:
        double acceptCash(double money)
        {
            return money;
        }
    };
    
    //打折收费类
    class CashRebate : public CashSuper
    {
    private :
        double rebate;
    public :
        CashRebate(){};
        CashRebate(double rebate)
        {
            this->rebate = rebate;
        }
    public:
        double acceptCash(double money)
        {
            return money * rebate;
        }
    };
    
    //返利收费类
    class CashReturn : public CashSuper
    {
    private :
        double moneyContion;
        double moneyReturn;
    public :
    
        CashReturn(){};
        CashReturn(double contion, double ret)
        {
            this->moneyContion = contion;
            this->moneyReturn = ret;
        }
    
        double acceptCash(double money)
        {
            double result = money;
            if(money > this->moneyContion)
            {
                result = money - this->moneyReturn * int(money / this->moneyContion);
            }
            return result;
        }
    };
    
    //收费工厂类
    class CashFactory
    {
    public :
        static CashSuper* createCashAccept(char* type)
        {
            char* str1 = "正常收费";
            char* str2 = "满300返100";
            char* str3 = "打八折";
    
            CashSuper* cs = 0;
            if(strcmp(type, str1) == 0)
            {
                cs = new CashNormal;
            }        
            else if(strcmp(type, str2) == 0)
            {
                cs = new CashReturn(300, 100);
            }
            else if(strcmp(type, str3) == 0)
            {
                cs = new CashRebate(0.8);
            }
    
            return cs;
        }
    
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        char* type[3] = {"正常收费", "满300返100", "打八折"};
        cout<<"选择收费方式"<<endl;
        for(int i=0; i<3; i++)
        {
            cout<<i<<" : "<<type[i]<<endl;
        }
        int num;
        cin>>num;
        
        double money;
        cout<<"请输入消费总额:"<<endl;
        cin>>money;
        
    
        CashSuper* cs = CashFactory::createCashAccept(type[num % 3]);
        money = cs->acceptCash(money);
        cout<<"应收金额为:"<<money<<endl;
    
        system("pause");
        return 0;
    }

     3.策略与简单工厂结合

    // StrategyTest03.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    
    #include <iostream>
    using namespace std;
    
    //现金收费抽象类
    class CashSuper
    {
    public:
        virtual double acceptCash(double money) = 0;
    };
    
    //正常收费类
    class CashNormal : public CashSuper
    {
    public:
        double acceptCash(double money)
        {
            return money;
        }
    };
    
    //打折收费类
    class CashRebate : public CashSuper
    {
    private :
        double rebate;
    public :
        CashRebate(){};
        CashRebate(double rebate)
        {
            this->rebate = rebate;
        }
    public:
        double acceptCash(double money)
        {
            return money * rebate;
        }
    };
    
    //返利收费类
    class CashReturn : public CashSuper
    {
    private :
        double moneyContion;
        double moneyReturn;
    public :
    
        CashReturn(){};
        CashReturn(double contion, double ret)
        {
            this->moneyContion = contion;
            this->moneyReturn = ret;
        }
    
        double acceptCash(double money)
        {
            double result = money;
            if(money > this->moneyContion)
            {
                result = money - this->moneyReturn * int(money / this->moneyContion);
            }
            return result;
        }
    };
    class CashContext
    {
    public :
        CashSuper* cs;
        CashContext(char* type)
        {
            char* str1 = "正常收费";
            char* str2 = "满300返100";
            char* str3 = "打八折";
    
            CashSuper* cs = 0;
            if(strcmp(type, str1) == 0)
            {
                cs = new CashNormal;
            }        
            else if(strcmp(type, str2) == 0)
            {
                cs = new CashReturn(300, 100);
            }
            else if(strcmp(type, str3) == 0)
            {
                cs = new CashRebate(0.8);
            }
        }
    
        double GetResult(double money)
        {
            return cs->acceptCash(money);
        }
    };
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        char* type[3] = {"正常收费", "满300返100", "打八折"};
        cout<<"选择收费方式"<<endl;
        for(int i=0; i<3; i++)
        {
            cout<<i<<" : "<<type[i]<<endl;
        }
        int num;
        cin>>num;
        
        double money;
        cout<<"请输入消费总额:"<<endl;
        cin>>money;
        
        CashContext cc(type[num % 3]);
        money = cc.GetResult(money);
    
        cout<<"应收金额为:"<<money<<endl;
    
        system("pause");
        return 0;
    }
  • 相关阅读:
    C# Asp.net中简单操作MongoDB数据库(二)
    Windows下使用TeamViewer连接远程服务器,以及解决“远程桌面关闭后TeamViewer不能连接”的问题
    存储过程
    C# 使用MongoDB遇到的问题
    C# Asp.net中简单操作MongoDB数据库(一)
    Windows下MongoDB设置用户、密码
    退役狗也要搬博客
    codeforces840E In a Trap
    uoj207共价大爷游长沙
    bzoj2662: [BeiJing wc2012]冻结 最短路 建图
  • 原文地址:https://www.cnblogs.com/sdlypyzq/p/2637963.html
Copyright © 2020-2023  润新知