大话数据模式(2) 商场促销-策略模式
2.1 商场收银软件
简单实现,当增加新的打折手段时,又要重新修改代码
2.2 增加打折
使用switch 与 下拉框简单增加功能实现,当增加功能时,还是需要重写代码
2.3 简单工厂实现
使用工厂模式将打一折到打九折整合到一起
面向对象的编程,并不是类越多越好,类的划分是为了封装,但是分类的基础是抽象,具有相同属性和功能的对象的抽象集合才是类
但是,当增加全新的非打折功能时,之前的工厂类就需要重写,这里可以有更好的解决办法
1 package com.zjk.code2Strategy; 2 3 /** 4 * @author zhoujiankang5 5 * @date 2020/9/2 18:23 6 */ 7 public abstract class CashSuper { 8 public abstract double acceptCash(double money); 9 } 10 11 class CashNormal extends CashSuper{ 12 13 @Override 14 public double acceptCash(double money) { 15 return money; 16 } 17 } 18 19 class CashRebate extends CashSuper{ 20 private double moneyRebate=1.0d; 21 22 public CashRebate(double moneyRebate) { 23 this.moneyRebate = moneyRebate; 24 } 25 26 @Override 27 public double acceptCash(double money) { 28 return money * moneyRebate; 29 } 30 } 31 32 class CashReturn extends CashSuper{ 33 private double moneyCondition=0.0d; 34 private double moneyReturn=0.0d; 35 36 public CashReturn(double moneyCondition, double moneyReturn) { 37 this.moneyCondition = moneyCondition; 38 this.moneyReturn = moneyReturn; 39 } 40 41 @Override 42 public double acceptCash(double money) { 43 double result=money; 44 if(money>=moneyCondition){ 45 return money-Math.floor(money/moneyCondition)*moneyReturn; 46 } 47 return result; 48 } 49 } 50 51 class CashFactory{ 52 public static CashSuper createCashAccept(String type){ 53 CashSuper cs= null; 54 switch (type){ 55 case "正常收费": 56 cs=new CashNormal(); 57 break; 58 case "满300返100": 59 cs=new CashReturn(300,100); 60 break; 61 case "打8折": 62 cs=new CashRebate(0.8); 63 break; 64 } 65 return cs; 66 } 67 } 68 69 class Agent{ 70 public static void main(String[] args) { 71 CashSuper cs=CashFactory.createCashAccept("打8折"); 72 double totalPrices=0.0d; 73 totalPrices=cs.acceptCash(100.0d); 74 System.out.println(totalPrices); 75 } 76 77 }
2.4 策略模式
定义算法家族,分别封装起来,让他们之间可以相互替换,此模式让算法的变化,不会影响到使用算法的客户
1 package com.zjk.code2Strategy; 2 3 /** 4 * @author zhoujiankang5 5 * @date 2020/9/2 18:40 6 */ 7 public abstract class Strategy { 8 public abstract void AlgorithmInterface(); 9 } 10 11 class ConcreteStrategyA extends Strategy{ 12 13 @Override 14 public void AlgorithmInterface() { 15 System.out.println("算法A"); 16 } 17 } 18 19 class ConcreteStrategyB extends Strategy{ 20 21 @Override 22 public void AlgorithmInterface() { 23 System.out.println("算法B"); 24 } 25 } 26 27 class ConcreteStrategyC extends Strategy{ 28 29 @Override 30 public void AlgorithmInterface() { 31 System.out.println("算法C"); 32 } 33 } 34 35 class Context{ 36 Strategy strategy; 37 38 public Context(Strategy strategy) { 39 this.strategy = strategy; 40 } 41 public void ContextInterface(){ 42 strategy.AlgorithmInterface(); 43 } 44 } 45 46 class Agent022{ 47 public static void main(String[] args) { 48 Context context; 49 context=new Context(new ConcreteStrategyA()); 50 context.ContextInterface(); 51 52 context=new Context(new ConcreteStrategyB()); 53 context.ContextInterface(); 54 55 context=new Context(new ConcreteStrategyC()); 56 context.ContextInterface(); 57 } 58 }
2.5 策略模式实现
CashSuper、CashNormal、CashRebate就是三个具体的策略(算法)
但是,当前只是用策略模式会出现判断使用哪一种算法,在客户端进行判断
2.6 策略与简单工厂
简单工厂模式我需要让客户端认识两个类【CashSuper和CashFactory】,而策略模式与简单工厂模式结合的用法,客户端只需要认识一个CashContext即可,耦合性更低
1 package com.zjk.code2Strategy; 2 3 /** 4 * @author zhoujiankang5 5 * @date 2020/9/4 19:31 6 */ 7 public class StrategyFactory { 8 public static void main(String[] args) { 9 CashContext cc=CashContext.createContext("打八折"); 10 System.out.printf("最终付款:"+cc.getResult(100.0d)); 11 } 12 } 13 14 class CashContext{ 15 CashSuper cs=null; 16 public CashContext(String type){ 17 switch (type){ 18 case "正常收费": 19 cs=new CashNormal(); 20 break; 21 case "满300返100": 22 cs=new CashReturn(300,100); 23 break; 24 case "打八折": 25 cs=new CashRebate(0.8); 26 break; 27 } 28 } 29 public double getResult(double money){ 30 return cs.acceptCash(money); 31 } 32 33 public static CashContext createContext(String type){ 34 return new CashContext(type); 35 } 36 }
2.7 策略模式解析
策略模式封装了变化!
探究未知是最大乐趣