大话设计模式之策略模式
来源:互联网 发布:舞蹈软件下载 编辑:程序博客网 时间:2024/04/26 09:33
应用案例:商场促销的多样性(折扣,满减)
package Strategy;//简单工厂模式实现public class CashFactory {public static CashSuper createCashAccept(String type) {CashSuper cs = null;switch(type) {case "正常收费":cs = new CashNormal();break;case "满300返100":cs = new CashReturn("300","100");break;case "打8折":cs = new CashRebate("0.8");break;}return cs;}/** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubdouble total = 0.0d,totalprice = 1000d;CashSuper csuper = CashFactory.createCashAccept("正常收费");total = csuper.acceptCash(totalprice);System.out.println(total);total = 0.0d;totalprice = 1000d;csuper = CashFactory.createCashAccept("满300返100");total = csuper.acceptCash(totalprice);System.out.println(total);total = 0.0d;totalprice = 1000d;csuper = CashFactory.createCashAccept("打8折");total = csuper.acceptCash(totalprice);System.out.println(total);}}abstract class CashSuper {public abstract double acceptCash(double money);}class CashNormal extends CashSuper {@Overridepublic double acceptCash(double money) {return money;}}class CashRebate extends CashSuper {private double moneyRebate = 1d;public CashRebate(String moneyRebate) {this.moneyRebate = Double.parseDouble(moneyRebate);}@Overridepublic double acceptCash(double money) {return money * moneyRebate;}}class CashReturn extends CashSuper {private double moneyCondition = 0.0d;private double moneyReturn = 0.0d;public CashReturn(String moneyCondition, String moneyReturn) {this.moneyCondition = Double.parseDouble(moneyCondition);this.moneyReturn = Double.parseDouble(moneyReturn);}@Overridepublic double acceptCash(double money) {double result = money;if (money >= moneyCondition) {result = money - Math.floor(money/this.moneyCondition) * this.moneyReturn; }return result;}}
采用简单工厂模式的缺陷:促销方式的修改经常需要修改工厂,代码需要经常重新编译部署代码
策略模式:它定义了算法家族,分别封装起来,让它们之间可以相互替换,此模式让算法的变化,不会影响到使用的算法客户。
Context是一个配置管理类,持有一个Strategy抽象类对象,构造函数用于初始化该对象,Context封装了Strategy的抽象方法调用。
简言之,以算法类为参数配置管理类,配置管理类对算法调用进行了封装
package Strategy;abstract class CashSuper {public abstract double acceptCash(double money);}class CashNormal extends CashSuper {@Overridepublic double acceptCash(double money) {return money;}}class CashRebate extends CashSuper {private double moneyRebate = 1d;public CashRebate(String moneyRebate) {this.moneyRebate = Double.parseDouble(moneyRebate);}@Overridepublic double acceptCash(double money) {return money * moneyRebate;}}class CashReturn extends CashSuper {private double moneyCondition = 0.0d;private double moneyReturn = 0.0d;public CashReturn(String moneyCondition, String moneyReturn) {this.moneyCondition = Double.parseDouble(moneyCondition);this.moneyReturn = Double.parseDouble(moneyReturn);}@Overridepublic double acceptCash(double money) {double result = money;if (money >= moneyCondition) {result = money - Math.floor(money / this.moneyCondition)* this.moneyReturn;}return result;}}public class CashContext {private CashSuper cs;public CashContext(CashSuper csuper) {this.cs = csuper;}public double GetResult(double money) {return cs.acceptCash(money);}public static void main(String[] args) {// TODO Auto-generated method stubCashContext cc = null;String type = "正常收费";switch(type) {case "正常收费":cc = new CashContext(new CashNormal());break;case "满300返100":cc = new CashContext(new CashReturn("300","100"));break;case "打8折":cc = new CashContext(new CashRebate("0.8"));break;}double total = 0.0d,totalprice = 1000d;total = cc.GetResult(totalprice);System.out.println(total);total = 0.0d;totalprice = 1000d;cc = new CashContext(new CashReturn("300","100"));total = cc.GetResult(totalprice);System.out.println(total);total = 0.0d;totalprice = 1000d;cc = new CashContext(new CashRebate("0.8"));total = cc.GetResult(totalprice);System.out.println(total);}}
策略与简单工厂的结合:
还是通过配置类,配置算法类,只是在内部用工厂方法产生
package Strategy;abstract class CashSuper {public abstract double acceptCash(double money);}class CashNormal extends CashSuper {@Overridepublic double acceptCash(double money) {return money;}}class CashRebate extends CashSuper {private double moneyRebate = 1d;public CashRebate(String moneyRebate) {this.moneyRebate = Double.parseDouble(moneyRebate);}@Overridepublic double acceptCash(double money) {return money * moneyRebate;}}class CashReturn extends CashSuper {private double moneyCondition = 0.0d;private double moneyReturn = 0.0d;public CashReturn(String moneyCondition, String moneyReturn) {this.moneyCondition = Double.parseDouble(moneyCondition);this.moneyReturn = Double.parseDouble(moneyReturn);}@Overridepublic double acceptCash(double money) {double result = money;if (money >= moneyCondition) {result = money - Math.floor(money / this.moneyCondition)* this.moneyReturn;}return result;}}public class CashContext {private CashSuper cs;public CashContext(String type) {switch(type) {case "正常收费":cs = new CashNormal();break;case "满300返100":cs = new CashReturn("300","100");break;case "打8折":cs =new CashRebate("0.8");break;}}public double GetResult(double money) {return cs.acceptCash(money);}public static void main(String[] args) {// TODO Auto-generated method stubCashContext cc = null;double total = 0.0d,totalprice = 1000d;cc = new CashContext("正常收费");total = cc.GetResult(totalprice);System.out.println(total);total = 0.0d;totalprice = 1000d;cc = new CashContext("满300返100");total = cc.GetResult(totalprice);System.out.println(total);total = 0.0d;totalprice = 1000d;cc = new CashContext("打8折");total = cc.GetResult(totalprice);System.out.println(total);}}
- 大话设计之策略模式
- 大话设计模式之策略模式代码
- 大话设计模式之策略模式
- 大话设计模式之策略模式
- 大话设计模式之策略模式
- 大话设计模式之策略模式
- 大话设计模式之策略模式
- C++大话设计模式之策略模式
- 大话设计模式读书笔记之策略模式
- 二、大话设计模式 之 策略模式
- 大话设计模式之策略模式
- 大话设计模式之策略模式
- 大话设计模式读后感之策略模式
- 大话设计模式读书笔记之策略模式
- 《大话设计模式》之策略模式
- 大话设计模式-策略模式
- 大话设计模式-策略模式
- 大话设计模式:策略模式
- 1018. Public Bike Management (30)-PAT
- poj 1679 The Unique MST (判断最小生成树是否为一)
- android垂直seekbar
- Tree 小结
- java验证码识别--3
- 大话设计模式之策略模式
- 流媒体Live555(四)——LiveMediaServer流程分析
- Ant 构建文件的一种写法
- jdbc的连接
- Hand to hand teach you load testing web site use Visual studio.
- 流媒体Live555(五)——openRTSP流程分析
- 更好地生活--睡眠
- AE开发中实现Control中的各种图形工具的方法
- java访问sql 2008 检索自动生成的键,获得 IDENTITY 值的方法