zoukankan      html  css  js  c++  java
  • 二 策略模式

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

    策略模式封装了变化

    在实践中,我们发现可以用它来封装几乎任类型的规则,只要在分析过程中听到需要在不同时间应用到不同的业务规则,就考虑使用策略模式来处理这种变化的可能

    商场促销例子

    现金收费抽象类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace factoryClass.Strategy
    {
        /// <summary>
        /// 现金收费抽象类
        /// </summary>
       public abstract class CashSuper
        {
           /// <summary>
           /// 超类的抽象方法,收取现金
           /// </summary>
            /// <param name="money">参数为原价</param>
            /// <returns>返回为当前价</returns>
           public abstract double acceptCash(double money);
        }
    }
    View Code

    正常收费子类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace factoryClass.Strategy
    {
        /// <summary>
        /// 正常收费子类
        /// </summary>
       public class CashNormal:CashSuper
        {
            public override double acceptCash(double money)
            {
                return money;
            }
        }
    }
    View Code

    打折收费子类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace factoryClass.Strategy
    {
        /// <summary>
        /// 打折收费子类
        /// </summary>
       public class CashRebate:CashSuper
        {
           private double moneyRebate = 1d;
           public CashRebate(string moneyRebate)
           {
               this.moneyRebate = double.Parse(moneyRebate);
           }
            public override double acceptCash(double money)
            {
                return moneyRebate * money;
            }
        }
    }
    View Code

    返利收费子类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace factoryClass.Strategy
    {
        /// <summary>
        /// 返利收费子类
        /// </summary>
      public  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 resurt = money;
                if (money >= moneyCondition)
                {
                    resurt = money - Math.Floor(money / moneyCondition) * moneyReturn;
                }
                return resurt;
            }
        }
    }
    View Code

    CashContext类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace factoryClass.Strategy
    {
       public class CashContext
        {
            CashSuper cs=null;//申明一个CashSuper对象
    
           /// <summary>
            /// CashContext
           /// </summary>
           /// <param name="type">促销类型</param>
           /// <param name="moneyRebates">折扣</param>
           /// <param name="moneyCondition"></param>
           /// <param name="moneyReturn"></param>
            public CashContext(string type, string moneyRebates, string moneyCondition, string moneyReturn)
           {
               switch (type)
               {
                   case "0":
                       CashNormal cs0 = new CashNormal();
                       cs = cs0;
                       break;
                   case "1":
                       CashRebate cs1 = new CashRebate(moneyRebates);
                       cs = cs1;
                       break;
                   case "2":
                       CashReturn cs2 = new CashReturn(moneyCondition, moneyReturn);
                       cs = cs2;
                       break;
    
               }
           }
    
           /// <summary>
           /// 根据收费策略不同,获得计算结果
           /// </summary>
           /// <param name="money"></param>
           /// <returns></returns>
           public double GetResult(double money)
           {
               return cs.acceptCash(money);
           }
        }
    }
    View Code

    客户端

     protected void btnA_Click(object sender, EventArgs e)
            {
                factoryClass.Strategy.CashContext cashContext = new factoryClass.Strategy.CashContext(this.ddlcu.SelectedValue,this.ddlzhe.SelectedValue,this.txtman.Text,this.txtfan.Text);
                this.txtying.Text = cashContext.GetResult(Convert.ToDouble(this.txtprice.Text)).ToString();
            }
    View Code
  • 相关阅读:
    Win7 IE11无法打开的可能解决办法
    s​q​l​ ​s​e​r​v​e​r​ ​2​0​0​0​登​录​名​与​数​据​库​用​户​名​的​关​联​问​题
    错误 0xc0202049: 数据流任务 1: 无法在只读列“ID”中插入数据
    清空SQL Server数据库中所有表数据的方法
    01-鼠标点击空白处实现层隐藏
    01-artDialog4.1.7常用整理
    ASP.NET MVC HtmlHelper用法大全
    随机生成十个数 填充数组
    字串加密、解密
    动手动脑、String类函数的使用说明
  • 原文地址:https://www.cnblogs.com/sl372/p/4546824.html
Copyright © 2011-2022 走看看