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

    1. 简介

    策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。

    2. 组成

    —抽象策略角色: 策略类,通常由一个接口或者抽象类实现。

    —具体策略角色:包装了相关的算法和行为。

    —环境角色:持有一个策略类的引用,最终给客户端调用。

    3. 程序案例

    案例:做一个商场收银软件,银业缘根据客户所购商品的单价和数量,向客户收费。

    程序示例:

      1 //现金收费抽象类
      2     abstract class CashSuper 
      3     {
      4         //现金收取超类的抽象方法,收取现金,参数为原价,返回为当前价
      5         public abstract double acceptCash(double money);
      6     }
    7 //正常收费子类 8 class CashNormal:CashSuper 9 { 10 //正常收费,原价返回 11 public override double acceptCash(double money) 12 { 13 return money; 14 } 15    }
    16 //打折收费子类 17 class CashRebate:CashSuper 18 { 19 private double moneyRebate = 1d; 20 //打折收费,初始化时,必须要输入折扣率,如八折,就是0.8 21 public CashRebate(string moneyRebate) 22 { 23 this.moneyRebate = double.Parse(moneyRebate); 24 } 25 public override double acceptCash(double money) 26 { 27 return money * moneyRebate; 28 } 29    }
    30 //返利收费子类 31 class CashReturn:CashSuper 32 { 33 private double moneyCondition = 0.0d; 34 private double moneyReturn = 0.0d; 35 //返利收费,初始化时必须要输入返利条件和返利值 36 //比如满300返100,则moneyCondition为300,moneyReturn为100 37 public CashReturn(string moneyCondition, string moneyReturn) 38 { 39 this.moneyCondition = double.Parse(moneyCondition); 40 this.moneyReturn = double.Parse(moneyReturn); 41 } 42 public override double acceptCash(double money) 43 { 44 double result = money; 45 //若大于返利条件则需要减去返利值 46 if (money >= moneyCondition) 47 { 48 result = money - Math.Floor(money/moneyCondition)*moneyReturn; 49 } 50 return result; 51 } 52    } 53 //只是策略模式 54 //class CashContext 55 //{ 56 // //声明一个CashSuper对象 57 // private CashSuper cs; 58 // //通过构造方法,传入具体的收费策略 59 // public CashContext(CashSuper csuper) 60 // { 61 // this.cs = csuper; 62 // } 63 // //根据收费策略的不同,获得计算结果 64 // public double GetResult(double money) 65 // { 66 // return cs.acceptCash(money); 67 // } 68 //} 69 70 //策略与简单工厂结合 71 class CashContext 72 { 73 CashSuper cs = null; 74 //注意参数不是具体的收费策略对象,而是一个字符串,表示收费类型 75 //将实例化具体策略的过程由客户端转移到Context类中。简单工厂的应用 76 public CashContext(string type) 77 { 78 switch (type) 79 { 80 case "正常收费": 81 CashNormal cs0 = new CashNormal(); 82 cs = cs0; 83 break; 84 case "满300返100": 85 CashReturn cs1 = new CashReturn("300","100"); 86 cs = cs1; 87 break; 88 case "打8折": 89 CashRebate cs2 = new CashRebate("0.8"); 90 cs = cs2; 91 break; 92 } 93 } 94 public double GetResult(double money) 95 { 96 return cs.acceptCash(money); 97 } 98    }
        
    99 //客户端调用 100 CashContext csuper = new CashContext(cbxType.SelectedItem.ToString()); 101 Double totalPrice = 0d; 102 totalPrice = csuper.GetResult(Convert.ToDouble(txtPrice.Text) * Convert.ToDouble(txtNum.Text));
  • 相关阅读:
    Python ctypes调用clib代码示例
    一点利用lme4包进行BLUP/BLUE计算的DEMO
    文献阅读 | Identifying barley pan-genome sequence anchors using genetic mapping and machine learning
    文献阅读 | Plant-ImputeDB: an integrated multiple plant reference panel database for genotype imputation
    文献阅读 | Genetic Diversity, Pedigree Relationships, and A Haplotype-Based DNA Fingerprinting System of Red Bayberry Cultivars
    文献阅读 | The Power of Inbreeding: NGS-Based GWAS of Rice Reveals Convergent Evolution during Rice Domestication
    文献阅读 | Worldwide phylogeography and history of wheat genetic diversity
    文献阅读 | RPAN: rice pan-genome browser for ∼3000 rice genomes
    tfidf代码简单实现
    conda 安装 graph-tool, 无需编译
  • 原文地址:https://www.cnblogs.com/HuoAA/p/4044741.html
Copyright © 2011-2022 走看看