设计一个商场收费系统,可能搞活动的时候会打折,或者满多少减多少等。
1。设计收费的父类
package com.dahua.cash;
/**
* Created by walle
* 2017/10/12.10:17
* good good study!
* 收费的父类
*/
public abstract class CashSuper {
public abstract double acceptCash(double money);
}
2。正常收费的子类
package com.dahua.cash;
/**
* Created by walle
* 2017/10/12.10:20
* good good study!
* 正常收费子类
*/
public class CashNormal extends CashSuper {
@Override
public double acceptCash(double money) {
return money;
}
}
3。打折的子类
package com.dahua.cash;
/**
* Created by walle
* 2017/10/12.10:23
* good good study!
*/
public class CashRebate extends CashSuper {
private double cashRebate;
public CashRebate(String cashRebate) {
this.cashRebate = Double.parseDouble(cashRebate);
}
@Override
public double acceptCash(double money) {
return money*cashRebate;
}
}
4。返利的子类
package com.dahua.cash;
/**
* Created by walle
* 2017/10/12.10:29
* good good study!
* 返利收费子类 例如满三百反一百
*/
public class CashReturn extends CashSuper {
private double moneyCondition;
private double moneyReturn;
public CashReturn(String moneyCondition, String moneyReturn) {
this.moneyCondition = Double.parseDouble(moneyCondition);
this.moneyReturn = Double.parseDouble(moneyReturn);
}
@Override
public double acceptCash(double money) {
double result = money;
if(money>=moneyCondition){
result=money-Math.floor(money/moneyCondition)*moneyReturn;
}
return 0;
}
public static void main(String[] args) {
double result = 340-Math.floor(340/300)*100;
System.out.println(Math.floor(340/300));
//floor 返回不大于的最大整数 round 四舍五入 ceil返回大于的最大整数
System.out.println(result);
}
}
5。工厂模式
package com.dahua.cash;
/**
* Created by walle
* 2017/10/12.10:46
* good good study!
* 收费工厂类
*/
public class CashFactory {
public static CashSuper createCashAccept(String type) {
CashSuper cashSuper = null;
switch (type) {
case “normal”:
cashSuper = new CashNormal();
break;
case “打8折”:
cashSuper = new CashRebate(“0.8”);
break;
case “满300反一百”:
cashSuper = new CashReturn(“300”, “100”);
break;
}
return cashSuper;
}
}
6。策略模式
package com.dahua.cash;
/**
* Created by walle
* 2017/10/12.11:23
* good good study!
*/
public class CashContext {
CashSuper cashSuper = null;
public CashContext(String type){
switch (type) {
case “normal”:
cashSuper = new CashNormal();
break;
case “打8折”:
cashSuper = new CashRebate(“0.8”);
break;
case “满300反一百”:
cashSuper = new CashReturn(“300”, “100”);
break;
}
}
public double getResult(double money){
return cashSuper.acceptCash(money);
}
}
7。客户端
package com.dahua.cash;
/**
* Created by walle
* 2017/10/12.10:53
* good good study!
* 收费客户端
*/
/**
* 策略模式和工厂模式的区别在于
* 简单工厂模式要识别两个类CashSuper和CashFactory
* 而策略模式只需要识别一个CashContext就行 ,耦合更低
* 让具体的算法和客户端分离。
*
* 策略模式减少了各种算法类和使用算法之间的耦合,把他们的公共的功能写到一个getResult方法
* 使得算法间有了公共的父类CashSuper
*/
public class CashClient {
public static void main(String args[]) {
double total = 0.00d;
//此处type可以根据下拉框来选择
// CashSuper cashSuper = CashFactory.createCashAccept(“打8折”);
// double totalPrices = cashSuper.acceptCash(200);
// total = total + totalPrices;
// System.out.println(total);
CashContext cashContext = new CashContext(“打8折”);
double totalPrices = cashContext.getResult(2 * 300);
total = total + totalPrices;
System.out.println(total);
}
}