先看以下范例:
public class Promotions {
public static void main(String[] args) {
Integer price=getPrice(3,1000);
System.out.println(price);
}
/**
* 不同的数字代表不同的打折方式
* @param num
* @param totalPrice
* @return
*/
private static Integer getPrice(int num, int totalPrice) {
Integer price=0;
switch (num){
case 1:
price=getOneZhePrice(totalPrice);
break;
case 2:
price=getTwoZhePrice(totalPrice);
break;
case 3:
price=getThreeZhePrice(totalPrice);
break;
default:
price=totalPrice;
}
return price;
}
private static Integer getThreeZhePrice(int totalPrice) {
return totalPrice*30;
}
private static Integer getTwoZhePrice(int totalPrice) {
return totalPrice*20;
}
private static Integer getOneZhePrice(int totalPrice) {
return totalPrice*10;
}
}
加入策略类,每种打折的实现在自己的策略类实现,定义计算价格的总策略类,具体的策略类继承总抽象类:
public abstract class CalculationPrice {
//定义获取价格的方法
public abstract Integer getPrice(Integer totalPrice);
}
public class OneCalculation extends CalculationPrice {
@Override
public Integer getPrice(Integer totalPrice) {
return totalPrice*1;
}
}
public class TwoCalculation extends CalculationPrice {
@Override
public Integer getPrice(Integer totalPrice) {
return totalPrice*2;
}
}
public class ThreeCalculation extends CalculationPrice{
@Override
public Integer getPrice(Integer totalPrice) {
return totalPrice*3;
}
}
public class Promotions {
public static void main(String[] args) {
Integer price=getPrice(1,1000);
System.out.println(price);
}
/**
* 不同的数字代表不同的打折方式
* @param num
* @param totalPrice
* @return
*/
private static Integer getPrice(int num, int totalPrice) {
CalculationPrice calculationPrice=null;
switch (num){
case 1:
calculationPrice=new OneCalculation();
break;
case 2:
calculationPrice=new TwoCalculation();
break;
case 3:
calculationPrice=new ThreeCalculation();
break;
default:
calculationPrice=new OneCalculation();
}
return calculationPrice.getPrice(totalPrice);
}
/*private static Integer getThreeZhePrice(int totalPrice) {
return totalPrice*30;
}
private static Integer getTwoZhePrice(int totalPrice) {
return totalPrice*20;
}
private static Integer getOneZhePrice(int totalPrice) {
return totalPrice*10;
}*/
}
加入工厂类:
public class PromotionsFactory {
//根据不同的策略获取不同的策略类
public static CalculationPrice getCalculationPrice(Integer integer){
CalculationPrice calculationPrice=null;
switch (integer){
case 1:
calculationPrice=new OneCalculation();
break;
case 2:
calculationPrice=new TwoCalculation();
break;
case 3:
calculationPrice=new ThreeCalculation();
break;
default:
calculationPrice=new OneCalculation();
}
return calculationPrice;
}
}
public class Promotions {
public static void main(String[] args) {
Integer price=getPrice(1,1000);
System.out.println(price);
}
/**
* 不同的数字代表不同的打折方式
* @param num
* @param totalPrice
* @return
*/
private static Integer getPrice(int num, int totalPrice) {
CalculationPrice calculationPrice = PromotionsFactory.getCalculationPrice(num);
return calculationPrice.getPrice(totalPrice);
/*CalculationPrice calculationPrice=null;
switch (num){
case 1:
calculationPrice=new OneCalculation();
break;
case 2:
calculationPrice=new TwoCalculation();
break;
case 3:
calculationPrice=new ThreeCalculation();
break;
default:
calculationPrice=new OneCalculation();
}
return calculationPrice.getPrice(totalPrice);*/
}
/*private static Integer getThreeZhePrice(int totalPrice) {
return totalPrice*30;
}
private static Integer getTwoZhePrice(int totalPrice) {
return totalPrice*20;
}
private static Integer getOneZhePrice(int totalPrice) {
return totalPrice*10;
}*/
}