标签:有助于 mon uml 判断 就是 继承 父类 int 职责
以商场收银为例,理解并实践“策略模式”。
一、面向过程的实现方式
1 package secondStrategy; 2 import java.text.DecimalFormat; 3 public class StrategyTest { 4 public static void main(String[] args) { 5 // 营业员提供信息 6 double total=0; 7 double price=2; 8 int num=3; 9 String promotions="打8折";//或者“满100-30” 10 11 // 计算待付金额 12 double totalPrice=price*num; 13 total=total+totalPrice; 14 15 switch (promotions) { 16 case "打8折":total=total*0.8;break; 17 case "满100-30": total=total-(total/100)*30; // "/"取整。“%”取余 18 default: 19 break; 20 } 21 DecimalFormat df = new DecimalFormat("#.##"); // 如果写成0.00,结果就一定是两个小数 22 System.out.println("单价:"+price+";数量:"+num+";原价:"+totalPrice+";活动:"+promotions+";活动价:"+df.format(total)); 23 24 } 25 }
缺点:当有新的活动时,就要不停地复制程序代码,新增一个类别,这使程序的重复性增加,可维护性变差。
二、通过简单工厂实现
1、UML图结构
2、语言描述
三、通过策略模式来设计
1、UML图结构
2、文字描述
四、策略模式与工厂模式相结合
1、UML图结构
2、文字描述
3、java代码实现
1 package secondStrategy; 2 3 public abstract class CashSuper { 4 public abstract double acceptCash(double money); 5 }
1 package secondStrategy; 2 3 public class CashNormal extends CashSuper { 4 5 @Override 6 public double acceptCash(double money) { 7 return money; 8 } 9 10 }
1 package secondStrategy; 2 3 // 打折类 4 public class CashRebate extends CashSuper { 5 private double moneyRebate=0; 6 // 构造方法,初始化时应传入折扣 7 public CashRebate(double moneyRe) { 8 this.moneyRebate=moneyRe; 9 } 10 @Override 11 public double acceptCash(double money) { 12 double result=0; 13 result=money*moneyRebate; 14 return result; 15 } 16 17 }
1 package secondStrategy; 2 3 public class CashReturn extends CashSuper { 4 private double moneyCondition=0; //满减门槛 5 private double moneyReturn=0; // 满减金额 6 7 public CashReturn(double moneyCon,double moneyRe) { 8 // TODO Auto-generated constructor stub 9 this.moneyCondition=moneyCon; 10 this.moneyReturn=moneyRe; 11 } 12 13 @Override 14 public double acceptCash(double money) { 15 double result=0; 16 result=money-(money/moneyCondition)*moneyReturn; 17 return result; 18 } 19 20 }
1 package secondStrategy; 2 3 public class CashContext { 4 private CashSuper cSuper; 5 public CashContext( String type) { 6 switch (type) { 7 case "打8折": this.cSuper=new CashRebate(0.8); break; 8 case "满100-30": this.cSuper=new CashReturn(100,30);break; 9 default:this.cSuper=new CashNormal(); 10 break; 11 } 12 } 13 public double getResult(double money){ 14 return this.cSuper.acceptCash(money); 15 } 16 }
package secondStrategy; import java.text.DecimalFormat; public class StrategyTest { public static void main(String[] args) { // 营业员提供信息 double total=0; double price=2; int num=3; String promotions="打8折";//或者“满100-30” // 计算待付金额 double totalPrice=price*num; total=total+totalPrice; switch (promotions) { case "打8折":total=total*0.8;break; case "满100-30": total=total-(total/100)*30; // "/"取整。“%”取余 default: break; } DecimalFormat df = new DecimalFormat("#.##"); // 如果写成0.00,结果就一定是两个小数 System.out.println("单价:"+price+";数量:"+num+";原价:"+totalPrice+";活动:"+promotions+";活动价:"+df.format(total)); } }
运行结果:
五、总结说明
4、特点:
标签:有助于 mon uml 判断 就是 继承 父类 int 职责
原文地址:https://www.cnblogs.com/zuolanlan/p/9990495.html