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
  • 相关阅读:
    PL/SQL Developer使用技巧、快捷键(转发)
    Java 获取随机日期
    jsonArray和Java List对象互转,日期处理
    ExtJs grid单选,多选
    ExtJs 下拉单联动,次级下拉框查询模式
    ExtJs 日期相加,Grid表格列可编辑
    转:Java阳历转农历
    转:Java 计算2个时间相差多少年,多少个月,多少天的几种方式
    钥匙计数之一
    LianLianKan
  • 原文地址:https://www.cnblogs.com/sl372/p/4546824.html
Copyright © 2011-2022 走看看