策略模式对应于解决某一个问题的一个算法族,容许用户从该算法族中任选一种算法解决一个问题,同时能够方便的更换算法或者增长新的算法。而且由客户端决定调用哪一个算法。算法
策略模式的本质:spring
分离算法,选择实现。ide
某公司市场部在接单时根据不一样的客户进行报价,能够划分为如下几种类型:测试
(1)新客户小批量报价this
(2)新客户大批量报价spa
(3)老客户小批量报价code
(4)老客户大批量报价对象
当遇到这种状况时,能够采用策略模式实现。blog
1 /** 2 * 普通方式实现报价 3 * @author CL 4 * 5 */ 6 public class TestStrategy { 7 8 public double getPrice(String type, double price) { 9 if (type.equals("新客户小批量")) { 10 System.out.println("不打折!"); 11 return price; 12 } 13 14 if (type.equals("新客户大批量")) { 15 System.out.println("打九折!"); 16 return price * 0.9; 17 } 18 19 if (type.equals("老客户小批量")) { 20 System.out.println("打八五折!"); 21 return price * 0.85; 22 } 23 24 if (type.equals("老客户大批量")) { 25 System.out.println("打八折!"); 26 return price * 0.8; 27 } 28 29 return price; 30 } 31 32 public static void main(String[] args) { 33 TestStrategy strategy = new TestStrategy(); 34 35 System.out.printf("您该报价:%6.2f", strategy.getPrice("老客户小批量", 998)); 36 37 System.out.println("\n---------------------------"); 38 39 System.out.printf("您该报价:%6.2f", strategy.getPrice("新客户大批量", 1024)); 40 } 41 42 }
控制台输出:开发
打八五折! 您该报价:848.30 --------------------------- 打九折! 您该报价:921.60
注意:实现起来很容易,符号通常开发人员的思路。可是,假如类型不少,算法比较复杂时,整个条件语句的代码就变得很长,难于维护。若是有新增类型,就须要频繁的修改代码。
不符合开闭原则!
1 /** 2 * 策略模式 3 * @author CL 4 * 5 */ 6 public interface Strategy { 7 8 public double getPrice(double originalCost); 9 10 }
1 /** 2 * 新客户小批量 3 * @author CL 4 * 5 */ 6 public class NewCustomerFewStrategy implements Strategy { 7 8 @Override 9 public double getPrice(double originalCost) { 10 System.out.println("不打折!"); 11 return originalCost; 12 } 13 14 }
1 /** 2 * 新客户大批量 3 * @author CL 4 * 5 */ 6 public class NewCustomerManyStrategy implements Strategy { 7 8 @Override 9 public double getPrice(double originalCost) { 10 System.out.println("打九折!"); 11 return originalCost * 0.9; 12 } 13 14 }
1 /** 2 * 老客户小批量 3 * @author CL 4 * 5 */ 6 public class OldCustomerFewStrategy implements Strategy { 7 8 @Override 9 public double getPrice(double originalCost) { 10 System.out.println("打八五折!"); 11 return originalCost * 0.85; 12 } 13 14 }
1 /** 2 * 老客户大批量 3 * @author CL 4 * 5 */ 6 public class OldCustomerManyStrategy implements Strategy { 7 8 @Override 9 public double getPrice(double originalCost) { 10 System.out.println("打八折!"); 11 return originalCost * 0.8; 12 } 13 14 }
1 /** 2 * 负责和具体的策略类交互 3 * 使策略模式,使具体的算法和直接的客户调用分离,使算法能够独立于客户端进行独立变化。 4 * 能够经过构造器注入策略对象的引用,也能够经过set方法注入策略对象的引用。 5 * 若是使用spring的依赖注入功能,还能够经过配置文件,动态地注入不一样的策略对象,动态的切换不一样的算法。 6 * @author CL 7 * 8 */ 9 public class Context { 10 11 private Strategy strategy; 12 13 //经过构造器注入 14 public Context(Strategy strategy) { 15 this.strategy = strategy; 16 } 17 18 //经过set方法注入 19 public void setStrategy(Strategy strategy) { 20 this.strategy = strategy; 21 } 22 23 public void printPrice(double originalCost) { 24 System.out.printf("您该报价:%6.2f", strategy.getPrice(originalCost)); 25 } 26 27 }
测试:
1 /** 2 * 测试策略模式 3 * @author CL 4 * 5 */ 6 public class Client { 7 8 public static void main(String[] args) { 9 Strategy strategy = new OldCustomerFewStrategy(); //老客户小批量 10 Context context = new Context(strategy); 11 12 context.printPrice(998); 13 14 System.out.println("\n---------------------------"); 15 16 Strategy strategy2 = new NewCustomerManyStrategy(); //新客户大批量 17 Context context2 = new Context(strategy2); 18 19 context2.printPrice(1024); 20 21 } 22 }
控制台输出:
打八五折! 您该报价:848.30 --------------------------- 打九折! 您该报价:921.60
(1)市场系统中的报价功能;
(2)医保系统中根据不一样的人缴纳不一样的保险费用;
(3)…………