JAVA策略模式-2015年下半年考试真题

大型商场要求商店有三种策略[原价、打折、满减]


现采用策略模式(strategy)实现该要求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//枚举三种策略
enum TYPE {NORMAL,CASH_DISCOUNT,CASH_RETURN}
interface CashSuper{
public double acceptCash(double money);
}
//原价
class CashNormal implements CashSuper{
public double acceptCash(double money){
return money;
}
}
//打折
class CashDiscount implements CashSuper{
private double discountRate;
public CashDiscount(double discountRate){
this.discountRate=discountRate;
}
public double acceptCash(double money){
return money*discountRate;
}
}
//满减
class CashReturn implements CashSuper{
private double moneyCondition;
private double moneyReturn;
public CashReturn(double moneyCondition,double moneyReturn){
this.moneyCondition=moneyCondition;
this.moneyReturn=moneyReturn;
}
public double acceptCash(double money){
double result =money;
if(money>=moneyCondition){
result=money-moneyReturn;
}
return result;
}
}
// 策略
public class CashContent {
private CashSuper sc;
private TYPE t;
public CashContent (TYPE t){
switch (t){
case NORMAL:
sc=new CashNormal();
break;
case CASH_DISCOUNT:
sc=new CashDiscount(0.9);
break;
case CASH_RETURN:
sc=new CashReturn(300.00,50.00);
break;
}
}
//实现
public static void main(String[] argus){
CashContent cc=new CashContent(TYPE.CASH_RETURN);
//CashContent cc=new CashContent(TYPE.CASH_DISCOUNT);
//CashContent cc=new CashContent(TYPE.NORMAL);
System.out.println(cc.sc.acceptCash(900));//结果为900-50=850.0
}
}

类图

策略模式