【大话设计模式读书笔记——策略模式】

策略模式

策略模式作为一种软件设计模式,指对象有某个行为,但是在不同的场景中,该行为有不同的实现算法。比如每个人都要“交个人所得税”,但是“在美国交个人所得税”和“在中国交个人所得税”就有不同的算税方法。
策略模式的特点:
1.定义了一族算法(业务规则);
2.封装了每个算法;
3.这族的算法可互换代替(interchangeable)。
维基百科-策略模式

策略模式实现商场打折

 /// 
    /// 收费基类
    /// 
    public abstract class CashSuper
    {
        public abstract double acceptCash(double money);
    }

    /// 
    /// 正常收费类
    /// 
    public class CashNormal : CashSuper
    {
        public override double acceptCash(double money)
        {
            return money;//正常收费,返回原价
        }
    }

    /// 
    /// 折扣类
    /// 
    public class CashRebate : CashSuper
    {
        private double _moneyRebate = 1d;//折扣

        public CashRebate(double moneyRebate)
        {
            this._moneyRebate = moneyRebate;
        }

        public override double acceptCash(double money)
        {
            return money * _moneyRebate;
        }
    }
    /// 
    /// 返利收费(即满X元,返利Y元)
    /// 
    public class CashReturn : CashSuper
    {
        private double _moneyCondition = 0.0d;//满X元
        private double _moneyReturn = 0.0d;//返利Y元

        public CashReturn(string moneyCondition, string moneyReturn)
        {
            this._moneyCondition = double.Parse(moneyCondition);
            this._moneyReturn = double.Parse(moneyReturn);
        }

        public override double acceptCash(double money)
        {
            double result = money;
            if (money >= _moneyCondition)
            {
                result = money - ((money / _moneyReturn) * _moneyReturn);//计算总金额减去返利金额之后的值
            }
            return result;
        }
    }

    /// 
    /// 获取收费方式的工厂类
    /// 
    public class CashFactory
    {
        public static CashSuper CreateCashAccept(string type)
        {
            CashSuper cashSuper = null;
            switch (type)
            {
                case "正常收费":
                    cashSuper = new CashNormal();
                    break;
                case "满300减100":
                    cashSuper = new CashReturn("300", "100");
                    break;
                case "折扣":
                    cashSuper = new CashRebate(0.8);
                    break;
            }
            return cashSuper;
        }
    }

    /// 
    /// 收费上下文
    /// 
    public class CashContext
    {
        private readonly CashSuper cash;

        /// 
        /// 构造方法传入具体的收费策略
        /// 
        /// 收费方式
        public CashContext(string type)
        {
            switch (type)
            {
                case "正常收费":
                    cash = new CashNormal();
                    break;
                case "满300减100":
                    cash = new CashReturn("300", "100");
                    break;
                case "折扣":
                    cash = new CashRebate(0.8);
                    break;
            }
        }

        /// 
        /// 计算结果值
        /// 
        /// 
        /// 
        public double GetResult(double money)
        {
            if (cash != null) return cash.acceptCash(money);
            return -1;
        }
    }

客户端调用

            var cash = new CashContext("折扣");//对外只用暴露一个类
            var result = cash.GetResult(500);
            Console.WriteLine(result);
            Console.ReadLine();

转载于:https://www.cnblogs.com/q975261413/p/8117869.html

你可能感兴趣的:(【大话设计模式读书笔记——策略模式】)