设计模式-状态模式
状态模式
问题
1 | 请编写程序完成APP抽奖活动具体要求如下: |
基本介绍
- 状态模式(State Pattern) :它主要用来解决对象在多种状态转换时,需要对外输出不同的行为的问题。状态和行为是一一对应的,状态之间可以相互转换。
- 当一个对象的丙在状态改变时,允许改变其行为,这个对象看起来像是改变了其类

- 环境类(Context)角色:也称为上下文,它定义了客户端需要的接口,内部维护一个当前状态,并负责具体状态的切换。
- 抽象状态(State)角色:定义一个接口,用以封装环境对象中的特定状态所对应的行为,可以有一个或多个行为。
- 具体状态(Concrete State)角色:实现抽象状态所对应的行为,并且在需要的情况下进行状态切换。
解决问题
- 状态接口
1 | public interface State { |
具体状态
不能抽奖
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
29public class NoRaffleState implements State {
//初始化时传入活动引用,扣除积分改变状态
Activity activity;
public NoRaffleState(Activity activity) {
this.activity = activity;
}
//当前状态可以扣积分,扣除后,将状态设置为可以抽奖
public void deduceMoney() {
System.out.println("扣除50积分成功,您可以抽奖了");
activity.setState(activity.getCanRaffleState());
}
//不能抽奖
public boolean raffle() {
System.out.println("扣除积分才能抽奖");
return false;
}
//
public void dispensePrize() {
System.out.println("不能发奖品");
}
}可以抽奖状态
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
35public class CanRaffleState implements State{
//初始化时传入活动引用,扣除积分改变状态
Activity activity;
public CanRaffleState(Activity activity) {
this.activity = activity;
}
public void deduceMoney() {
System.out.println("已经扣取过了积分");
}
public boolean raffle() {
System.out.println("正在抽奖请稍等");
Random r = new Random();
int num = r.nextInt(10);
System.out.println(num);
if (num == 0){
activity.setState(activity.getDispenseState());
return true;
}else{
System.out.println("很遗憾没有抽中奖品");
activity.setState(activity.getNoRaffleState());
return false;
}
}
public void dispensePrize() {
System.out.println("没中奖,不能发放奖品");
}
}发奖品的状态
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
32public class DispenseState implements State{
//初始化时传入活动引用,扣除积分改变状态
Activity activity;
public DispenseState(Activity activity) {
this.activity = activity;
}
public void deduceMoney() {
System.out.println("已经扣取过了积分");
}
public boolean raffle() {
System.out.println("不能抽奖");
return false;
}
public void dispensePrize() {
if (activity.getCount() > 0){
System.out.println("恭喜中奖了");
activity.setState(activity.getNoRaffleState());
}else {
System.out.println("奖品发完了");
//后面就不可以再
activity.setState(activity.getNoRaffleState());
}
}
}奖品发完的状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25public class DispenseOutState implements State {
//初始化时传入活动引用,扣除积分改变状态
Activity activity;
public DispenseOutState(Activity activity) {
this.activity = activity;
}
public void deduceMoney() {
System.out.println("奖品发送完了,请下次参加");
}
public boolean raffle() {
System.out.println("奖品发送完了,请下次参加");
return false;
}
public void dispensePrize() {
System.out.println("奖品发送完了,请下次参加");
}
}
抽奖活动类
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
64public class Activity {
//当前状态是变化的
State state = null;
//奖品数量
int count = 0;
//四个属性,表示4种状态
State noRaffleState= new NoRaffleState(this);
State canRaffleState= new CanRaffleState(this);
State dispenseState= new DispenseState(this);
State dispenseOutState= new DispenseOutState(this);
//初始化状态
public Activity(int count) {
this.state = getNoRaffleState();
this.count = count;
}
//扣分
public void deduceMoney(){
state.deduceMoney();
}
//抽奖
public void raffle(){
//抽奖成功,领取奖品
if (state.raffle()){
state.dispensePrize();
}
}
public void setState(State state) {
this.state = state;
}
public void setCount(int count) {
this.count = count;
}
//领取一次奖品
public int getCount() {
int curCount = count;
count--;
return curCount;
}
public State getNoRaffleState() {
return noRaffleState;
}
public State getCanRaffleState() {
return canRaffleState;
}
public State getDispenseState() {
return dispenseState;
}
public State getDispenseOutState() {
return dispenseOutState;
}
}客户端
1
2
3
4
5
6
7
8
9
10
11
12
13public class Client {
public static void main(String[] args) {
Activity activity = new Activity(1);
for (int i = 0; i < 30; i++){
System.out.println("第"+( i + 1)+"次抽奖");
//扣积分
activity.deduceMoney();
//抽奖
activity.raffle();
}
}
}
注意事项
- 代码有很强的可读性。状态模式将每个状态的行为封装到对应的一个类中
- 方便维护。将容易产生问题的if-else语句删除了,如果把每个状态的行为都放到一个类中,每次调用方法时都要判断当前是什么状态,不但会产出很多if-else语句,而且容易出错
- 符合“开闭原则”。容易增删状态
- 会产生很多类。每个状态都要一个对应的类,当状态过多时会产生很多类,加大维护难度
- 当一个事件或者对象有很多种状态,状态之间会相互转换,对不同的状态要求有不同的行为的时候,可以考虑使用状态模式