`

策略模式

 
阅读更多

一模式定义

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

 

二模式举例

1模式分析

我们借用商场促销商品来说明这一模式。



 

2策略模式静态类图



 

3代码示例

3.1 创建策略接口一IStrategy

Java代码  收藏代码
  1. package com.demo.strategy;  
  2.   
  3. /** 
  4.  * 策略接口 
  5.  *  
  6.  * @author 
  7.  *  
  8.  */  
  9. public interface IStrategy {  
  10.     /** 
  11.      * 计算实际价格方法 
  12.      *  
  13.      * @param consumePrice 
  14.      *            消费金额 
  15.      * @return 
  16.      */  
  17.     public double realPrice(double consumePrice);  
  18. }  

3.2 八折促销策略一RebateStrategy

Java代码  收藏代码
  1. package com.demo.strategy;  
  2.   
  3. /** 
  4.  * 打八折商品促销策略 
  5.  *  
  6.  * @author 
  7.  *  
  8.  */  
  9. public class RebateStrategy implements IStrategy {  
  10.     private final double rate;  
  11.   
  12.     /** 
  13.      * 构造方法设置打折率 
  14.      */  
  15.     public RebateStrategy() {  
  16.         this.rate = 0.8;  
  17.     }  
  18.   
  19.     /** 
  20.      * 计算实际价格方法 
  21.      *  
  22.      * @param consumePrice 
  23.      *            消费金额 
  24.      * @return 
  25.      */  
  26.     public double realPrice(double consumePrice) {  
  27.         return consumePrice * this.rate;  
  28.     }  
  29.   
  30. }  

3.3 满1000减200促销策略一ReduceStrategy

Java代码  收藏代码
  1. package com.demo.strategy;  
  2.   
  3. /** 
  4.  * 满1000减200 商品促销策略 
  5.  *  
  6.  * @author 
  7.  *  
  8.  */  
  9. public class ReduceStrategy implements IStrategy {  
  10.     /** 
  11.      * 计算实际价格方法 
  12.      *  
  13.      * @param consumePrice 
  14.      *            消费金额 
  15.      * @return 
  16.      */  
  17.     public double realPrice(double consumePrice) {  
  18.         if (consumePrice >= 1000) {  
  19.             return consumePrice - 200;  
  20.         } else {  
  21.             return consumePrice;  
  22.         }  
  23.     }  
  24. }  

3.4 200以上部分打8折促销策略一PromotionalStrategy

Java代码  收藏代码
  1. package com.demo.strategy;  
  2.   
  3. /** 
  4.  * 满200,高于200部分打八折 商品促销策略 
  5.  *  
  6.  * @author 
  7.  *  
  8.  */  
  9. public class PromotionalStrategy implements IStrategy {  
  10.     /** 
  11.      * 计算实际价格方法 
  12.      *  
  13.      * @param consumePrice 
  14.      *            消费金额 
  15.      * @return 
  16.      */  
  17.     public double realPrice(double consumePrice) {  
  18.         if (consumePrice > 200) {  
  19.             return 200 + (consumePrice - 200) * 0.8;  
  20.         } else {  
  21.             return consumePrice;  
  22.         }  
  23.   
  24.     }  
  25. }  

3.5 创建上下文环境一Context

Java代码  收藏代码
  1. package com.demo.context;  
  2.   
  3. import java.math.BigDecimal;  
  4.   
  5. import com.demo.strategy.IStrategy;  
  6.   
  7. /** 
  8.  * 上下文环境 
  9.  *  
  10.  * @author 
  11.  *  
  12.  */  
  13. public class Context {  
  14.     // 当前策略  
  15.     private IStrategy strategy;  
  16.   
  17.     // 设置当前策略  
  18.     public void setStrategy(IStrategy strategy) {  
  19.         this.strategy = strategy;  
  20.     }  
  21.   
  22.     // 使用策略计算价格  
  23.     public double cul(double consumePrice) {  
  24.         // 使用具体商品促销策略获得实际消费金额  
  25.         double realPrice = this.strategy.realPrice(consumePrice);  
  26.         // 格式化保留小数点后1位,即:精确到角  
  27.         BigDecimal bd = new BigDecimal(realPrice);  
  28.         bd = bd.setScale(1, BigDecimal.ROUND_DOWN);  
  29.         return bd.doubleValue();  
  30.     }  
  31. }  

3.6 消费者购物消费一Client

Java代码  收藏代码
  1. package com.demo;  
  2.   
  3. import java.util.Random;  
  4.   
  5. /** 
  6.  * 客户端应用程序 
  7.  *  
  8.  * @author 
  9.  *  
  10.  */  
  11. public class Client {  
  12.   
  13.     /** 
  14.      * @param args 
  15.      */  
  16.     public static void main(String[] args) {  
  17.         // 创建上下问环境对象实例  
  18.         // Context context = new Context();  
  19.         // 随机数对象  
  20.         Random random = new Random();  
  21.         for (int i = 0; i < 10; i++) {  
  22.             // 产生随机数的方式判断使用何种促销策略  
  23.             int x = random.nextInt(3);  
  24.             // 消费价格也是由随机数产生的(不能为0)  
  25.             double consumePrice = 0;  
  26.             while ((consumePrice = random.nextInt(2000)) == 0) {  
  27.             }  
  28.   
  29.             double realPrice = consumePrice;  
  30.             switch (x) {  
  31.             case 0:  
  32.                 // 打八折商品  
  33.                 // context.setStrategy(new RebateStrategy());  
  34.                 realPrice = consumePrice * 0.8;  
  35.                 break;  
  36.             case 1:  
  37.                 // 满200,高于200部分打八折 商品  
  38.                 // context.setStrategy(new PromotionalStrategy());  
  39.                 if (consumePrice > 200) {  
  40.                     realPrice = 200 + (consumePrice - 200) * 0.8;  
  41.                 }  
  42.                 break;  
  43.             case 2:  
  44.                 // 满1000减200 商品  
  45.                 // context.setStrategy(new ReduceStrategy());  
  46.                 if (consumePrice >= 1000) {  
  47.                     realPrice = consumePrice - 200;  
  48.                 }  
  49.                 break;  
  50.             }  
  51.             System.out.print("【"  
  52.                     + (x == 0 ? "打八折" : (x == 1 ? "高于200部分打八折"  
  53.                             : (x == 2 ? "满1000减200" : ""))) + "】商品:");  
  54.   
  55.             System.out.println("原价:" + consumePrice + " - 优惠后价格:" + realPrice);  
  56.         }  
  57.     }  
  58. }  

4运行结果

【满1000减200】商品:原价:908.0 - 优惠后价格:908.0

【满1000减200】商品:原价:1129.0 - 优惠后价格:929.0

【满1000减200】商品:原价:829.0 - 优惠后价格:829.0

【打八折】商品:原价:518.0 - 优惠后价格:414.40000000000003

【满1000减200】商品:原价:1230.0 - 优惠后价格:1030.0

【打八折】商品:原价:106.0 - 优惠后价格:84.80000000000001

【满1000减200】商品:原价:1134.0 - 优惠后价格:934.0

【高于200部分打八折】商品:原价:664.0 - 优惠后价格:571.2

【满1000减200】商品:原价:564.0 - 优惠后价格:564.0

【满1000减200】商品:原价:730.0 - 优惠后价格:730.0

 

三该模式设计原则

1"开-闭"原则

2单一职责原则

 

四使用场合

1当多个类的表现行为不同,需要在运行时刻动态选择具体执行的行为的时候。

2需要在不同情况下使用不同策略,或者策略还可能在未来用其它方式实现的时候。

3需要隐藏具体策略的实现细节,各个具体策略彼此独立的时候。

4当一个类中出现了多种行为,而且在一个操作中使用多个条件分支来判断使用多种行为的时候,可以使用策略模式将各个条件分支的动作植入具体策略中实现。

 

五策略模式静态类图



 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics