策略模式

来源:互联网 发布:ubuntu和linux的区别 编辑:程序博客网 时间:2024/06/05 18:39

介绍:

策略模式(Strategy)是一种定义一系列算法的方法,从概念上来看,所有这些算法完成的都是相同的工作,知识实现不同,它可以以相同的方式调用所有的算法,减少了各种算法类与使用算法类之间的耦合。 

Strategy结构图


 对上述结构图进行简单的解释说明:

        a、将所有的算法都抽象成了Strategy,可以将算法分离出来并且进行更换。
        b、Context 中含有对Strategy的引用。
        c、通过ContextInterface(),进行对算法的使用。

三种不同的算法具有抽象的父类Strategy


Strategy类

算法抽象类

<span style="font-size:14px;">     abstract class Strategy        {            //算法方法            public abstract void AlgorithmInterface();        }</span>
ConcreteStrategy,封装了具体的算法或行为,继承于Strategy

<span style="font-size:14px;"> //具体算法类        class ConcreteStrategy : Strategy        {            //算法A实现方法            public override void AlgorithmInterface();</span><span style="font-size: 18px;"></span><span style="font-size:14px;">            {            <span style="white-space: pre;"></span>Console.WriteLine("算法A类实现");            }<strong></strong>        }       class ConcreteStrategy : Strategy          {            //算法B实现方法            public override void AlgorithmInterface();            {</span><span style="font-size: 18px;"></span><span style="font-size:14px;">            Console.WriteLine("算法B类实现");            }          }       class ConcreteStrategy : Strategy        {            //算法C实现方法            public override void AlgorithmInterface();            {            Console.WriteLine("算法C类实现");            }        }</span>
Context,用一个ConcreteStrategy来配置,维护一个对Strategy对象的引用

<span style="font-size:14px;">        //上下文        class Context        {            Strategy strategy;            public Context(Strategy strategy)            {                this.strategy = strategy;            }            //上下文接口            public void ContextInterface()            {                strategy.AlgorithmInterface();            }        }</span>
客户端代码

<span style="font-size:14px;">   //由于实例化不同的策略,所以最终在调用context.ContextInterface();时;所获得的结果就不尽相同            ContextBoundObject context;            context=new Context(new ConcreteStrategyA()):            context.ContextInterface();            context=new Context(new ConcreteStrategyA()):            context.ContextInterface();            context=new Context(new ConcreteStrategyA()):            context.ContextInterface();            Console.Read ();</span>

商场收银系统的例子

使用策略模式的代码

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 策略模式_商场促销{    public partial class Form1 : Form    {        public Form1()        {            InitializeComponent();        }        // 现金收费抽象类        abstract class CashSuper        {            public abstract double acceptCash(double money);//现金收取类的抽象方法,收取现金,参数为原价,返回为当前价        }        //正常收费子类        class CashNormal : CashSuper        {            public override double acceptCash(double money)            {                return money;            }        }        //打折收费子类        class CashRebate : CashSuper        {            private double moneyRebate = 1d;            public CashRebate(string moneyRebate)            {                //打折收费,初始化时,必须要输入折扣率,如八折,就是0.8                this.moneyRebate = double.Parse(moneyRebate);            }            public override double acceptCash(double money)            {                return money * moneyRebate;            }        }        //返利收费子类        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.moneyCondition = double.Parse(moneyReturn);            }            public override double acceptCash(double money)            {                double result = money;                if (money >= moneyCondition)                    result = money - Math.Floor(money / moneyCondition) * moneyReturn;                return result;            }        }        //CashContext类        class CashContext        {            private CashSuper cs;   //声明一个CashSuper对象            public CashContext(CashSuper csuper)//通过构造函方法,传入具体的收费策略            {                this.cs = csuper;            }            public double GetResult(double money)            {                return cs.acceptCash(money);//根据收费策略的不同,获得计算结果            }        }        double total = 0.0d;//用于总计        private void btnok_Click(object sender, EventArgs e)        {            CashContext cc = null;            switch (cbxType.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(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));            total = total + totalPrices;            lbxList.Items.Add("单价: " + txtPrice.Text + " 数量:" + txtNum.Text + "" + cbxType.SelectedItem + " 合计:" + totalPrices.ToString());            lblResult.Text = total.ToString();        }    }}
效果


小结:

如果不使用任何模式的话,灵活性差。扩展性差如果需要增加某些功能的话,则需要改动整体的代码

使用简单工厂模式虽然能解决这个问题,但这个模式只是解决对象的创建问题,而且由于工厂本身包括了 所有的收费方式,商场是可能经常性地更改打折额度和返利额度,每次维护或扩展收费方式都要改动这个工厂,以致代码需要重新编译部署,这真的是很糟糕的。

0 0
原创粉丝点击