首页 > 代码库 > 大话设计模式_策略模式(Java代码)

大话设计模式_策略模式(Java代码)

策略模式:定义算法家族,分别封装,让它们之间可以互相替换,此模式让算法的变化不会影响到使用算法的客户

简单描述:一个父类,多个子类实现具体方法。一个Context类持有父类的引用(使用子类实例化此引用),客户端代码只需要与此Context类交互即可

大话设计模式中的截图:

例子代码:

策略类:

1 package com.longsheng.strategy;
2 
3 public abstract class Strategy {
4 
5     public abstract double getResult( double cash );
6     
7 }
Strategy

促销满减类:

 1 package com.longsheng.strategy;
 2 
 3 public class CashReturn extends Strategy {
 4 
 5     private double moneyCondition;
 6     private double moneyReturn;
 7 
 8     public CashReturn(double moneyCondition, double moneyReturn) {
 9         super();
10         this.moneyCondition = moneyCondition;
11         this.moneyReturn = moneyReturn;
12     }
13 
14     public double getMoneyCondition() {
15         return moneyCondition;
16     }
17 
18     public void setMoneyCondition(double moneyCondition) {
19         this.moneyCondition = moneyCondition;
20     }
21 
22     public double getMoneyReturn() {
23         return moneyReturn;
24     }
25 
26     public void setMoneyReturn(double moneyReturn) {
27         this.moneyReturn = moneyReturn;
28     }
29 
30     @Override
31     public double getResult(double cash) {
32         double result = cash;
33         if (cash >= moneyCondition) {
34             result = cash - (cash / moneyCondition) * moneyReturn;
35         }
36         return result;
37     }
38 
39 }
CashReturn

促销折扣类:

 1 package com.longsheng.strategy;
 2 
 3 public class CashRebate extends Strategy {
 4 
 5     private double discount;
 6 
 7     public double getDiscount() {
 8         return discount;
 9     }
10 
11     public void setDiscount(double discount) {
12         this.discount = discount;
13     }
14 
15     public CashRebate(double discount) {
16         this.discount = discount;
17     }
18 
19     @Override
20     public double getResult(double cash) {
21         return cash * getDiscount();
22     }
23 
24 }
CashRebate

正常结算类:

 1 package com.longsheng.strategy;
 2 
 3 public class CashNormal extends Strategy {
 4 
 5     @Override
 6     public double getResult( double cash ) {
 7         return cash;
 8     }
 9     
10 }
CashNormal

策略Context:(与简单工厂相结合)

 1 package com.longsheng.strategy;
 2 
 3 public class StrategyContext {
 4 
 5     private Strategy str;
 6 
 7     /**
 8      * a 代表正常收费; b 代表打7折; c 代表满500减100 
 9      * @param s
10      */
11     public StrategyContext(String s) {
12         switch (s) {
13         case "a":
14             this.str = new CashNormal();
15             break;
16         case "b":
17             this.str = new CashRebate(0.7d);
18             break;
19         case "c":
20             this.str = new CashReturn(500d, 100d);
21             break;
22         }
23     }
24 
25     public double getResult(double cash) {
26         return str.getResult(cash);
27     }
28 }
StrategyContext

客户端:

 1 package com.longsheng.strategy;
 2 
 3 //商场促销,打折、满减、满送积分等
 4 public class Client {
 5 
 6     public static void main(String[] args) {
 7         // a 代表正常收费; b 代表打7折; c 代表满500减100
 8         double total = 1000d;
 9         
10         StrategyContext sa = new StrategyContext("a");
11         System.out.println(sa.getResult(total));
12         
13         StrategyContext sb = new StrategyContext("b");
14         System.out.println(sb.getResult(total));
15         
16         StrategyContext sc = new StrategyContext("c");
17         System.out.println(sc.getResult(total));
18     }
19 
20 }
Client

在分析过程中听到需要在不同时间应用不同的业务规则,就可以考虑使用策略模式处理这种变化的可能性