设计模式二——策略模式



一、策略模式:策略模式是定义一系列算法的方法,所以的算法都是完成的相同的工作,只是实现的方法不同,它可以以相同的方式调用所有的算法,减少了各种算法与使用算法类之间的耦合

二、类图:

 

三、代码

//CashSuper的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式二__策略模式.Class
{
    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)
        {
            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.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;
        }
    }
}

//CashContext的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 设计模式二__策略模式.Class
{
    class CashContext
    {
        //声明一个CashSuper 对象
        CashSuper cs = null;
        //具体的对象为收费类型

        public CashContext(string type)
        {
            //将实例化具体策略的过程由客户端转移到Context类,减轻客户端的职责
            switch (type)
            { 
                case"正常收费":
                    CashNormal cs0 = new CashNormal();
                    cs = cs0;
                    break;
                case "满300返100":
                    cs = new CashReturn("300","100");
                    break;
                case "打八折":
                    cs = new CashRebate("0.8");
                    break;
            }
        }
        public double GetResult(double money)
        {
            return cs.acceptCash(money);
        }
    }
}

//客户端

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;
using 设计模式二__策略模式.Class;

namespace 设计模式二__策略模式
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        double total = 0.0d;

        private void GetMoney_Click(object sender, EventArgs e)
        {
            CashContext csuper = new CashContext(Typecbx.SelectedItem.ToString());
            double totalPrices = 0d;
            totalPrices = csuper.GetResult(Convert.ToDouble(Unittxt.Text.Trim())*Convert.ToDouble(couttxt.Text.Trim()));
            total += totalPrices;
            MessageBox.Show("总价:  "+total.ToString());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Typecbx.Items.AddRange(new object[]{"正常收费","打八折","满300返100"});
            Typecbx.SelectedIndex = 0;
        }
    }
}


四、优缺点
优点:
1. 策略模式的Strategy类层次为Context定义了一系列的可供重用的算法或行为,继承有助于析取出这些算法的公共功能。
2. 简化了单元测试,因为每一个算法都有自己的类,可以通过自己的接口单独测试
3. 修改某一个具体的算法不会影响到其他的算法
缺点:
1. 调用方法传递参数时,需要switch判断具体实现哪个方法,switch 判断可以放在客户端或者是策略模式的Context对象,一般来说,为了减轻客户端的职责,我们放在策略模式的Context对象内。

五、总结:策略模式用来封装算法,也可以封装几乎任何类型的规则,只要在分析过程中听到在不同时间应用不同的规则业务,就可以考虑策略模式处理这种变化的可能性。

引用:<<大话设计模式>> (程杰)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值