《大话设计模式》——策略模式1

来源:互联网 发布:网络枪战游戏 编辑:程序博客网 时间:2024/04/28 07:16
策略模式:用来封装具体算法。(降低了使用算法类与算法类的耦合度)

策略模式和简单工厂模式很像。都有很多子类(具体算法)。

策略模式是一种定义一系列算法的方法。
他首先定义了算法家族,从概念上看,所有这些算法完成的都是相同的工作,只是实现不同,这些算法可以通过相同的方式调用,减少了各种算法类与使用算法类之间的耦合。将这些算法封装成类,让他们可以互相替换。该模式让算法的变化,不会影响到使用算法的客户。

算法本身只是一种策略,这些算法随时都可能相互替换。这是变化点,而封装变化点是面向对象的一种很重要的思维。

策略模式的优点:降低了使用算法类与算法类的耦合度。同时这样也有利于算法段元的扩展及测试。

适用场合:解决同一件事,在不同的时间(情况)下,使用不同的业务规则时,就可以考虑使用策略模式。


using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace WindowsFormsApplication1{    /// <summary>    /// 收费抽象类    /// </summary>    abstract class CashSuper    {        public abstract double acceptCash(double money);    }    /// <summary>    /// 正常收费子类    /// </summary>    class CashNormal : CashSuper    {        public override double acceptCash(double money)        {            return money;        }    }    /// <summary>    /// 打折收费子类    /// </summary>    class CashRebate : CashSuper    {        private double moneyRebate = 1d;        public CashRebate(string moneyRebate)        {            this.moneyRebate = double.Parse(moneyRebate);        }        public override double acceptCash(double money)        {            return money * moneyRebate;        }    }    /// <summary>    /// 返利收费子类    /// </summary>    class CashReturn : CashSuper    {        private double moneyCondition = 0.0d;        private double moneyReturn = 0.0d;        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 - Math.Floor(money / moneyCondition) * moneyReturn;            return result;        }    }    /// <summary>    /// 收费工厂类    /// </summary>    class CashFactory    {        public static CashSuper createCashAccept(string type)        {            CashSuper cs = null;            switch (type)            {                 case "正常收费":                    cs = new CashNormal();                    break;                case "满300返100":                    cs = new CashReturn("300", "100");                    break;                case "打8折":                    cs = new CashRebate("0.8");                    break;            }            return cs;        }    }    /// <summary>    /// 策略上下文类    /// </summary>    class CashContext    {        //维护一个父类的引用        private CashSuper cs;        public CashContext(CashSuper csuper)        {            this.cs = csuper;        }        //对具体子类的方法进行封装        public double GetResult(double money)        {            return cs.acceptCash(money);        }    }    /// <summary>    /// 工厂与策略相结合的方式    /// </summary>    class CashContext2    {        CashSuper cs = null;        public CashContext2(string type)        {            switch (type)            {                case "正常收费":                    cs = new CashNormal();                    break;                case "满300返100":                    cs = new CashReturn("300", "100");                    break;                case "打8折":                    cs = new CashRebate("0.8");                    break;            }        }        public double GetResult(double money)        {            return cs.acceptCash(money);        }    }}

简单工厂模式需要客户端代码知道两个类,而策略模式与简单工厂结合客户端只需要知道一个类,耦合度更低。

        /// <summary>        /// 工厂模式客户端程序        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button1_Click(object sender, EventArgs e)        {            CashSuper csuper = CashFactory.createCashAccept(comboBox1.SelectedItem.ToString());            double totalPrices = 0d;            totalPrices = csuper.acceptCash(Convert.ToDouble(textBox1.Text) * Convert.ToDouble(textBox2.Text));            listBox1.Items.Add("单价:" + textBox1.Text + ";数量:" + textBox2.Text + ";收费模式:" + comboBox1.SelectedItem.ToString() + ";合计:" + totalPrices);        }        /// <summary>        /// 策略模式客户端程序        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button3_Click(object sender, EventArgs e)        {            CashContext cc = null;            switch (comboBox1.SelectedItem.ToString())            {                 case "正常收费":                    cc = new CashContext(new CashNormal());                    break;                case "满300返100":                    cc = new CashContext(new CashReturn("300","100"));                    break;                case "打8折":                    cc = new CashContext(new CashRebate("0.8"));                    break;            }            double totalPrices = 0d;            totalPrices = cc.GetResult(50d * 7d);        }        /// <summary>        /// 工厂与策略相结合的客户端程序        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button4_Click(object sender, EventArgs e)        {            CashContext2 csuper = new CashContext2(comboBox1.SelectedItem.ToString());            double totalPrices = 0d;            totalPrices = csuper.GetResult(50d * 7d);        }


0 0