0%

设计模式-状态模式

当一个控制对象状态转换的条件表达式过于复杂时, 将状态的判断逻辑转移到表示不同状态的一系列类中
当代码中包含大量与对象状态有关的条件语句时, 使用状态模式

状态模式

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
//抽象状态类
abstract class State
{
public abstract void Handle(Context context);
}
//具体状态类
class ConcreateStateA : State
{
public override void Handle(Context context)
{
context.State = new ConcreateStateB();
}
}
class ConcreateStateB : State
{
public override void Handle(Context context)
{
context.State = new ConcreateStateA();
}
}

class Context
{
private State state;
public Context(State state)
{
this.state = state;
}
public State State
{
get{ return state;}
set
{
state = value;
Console.WriteLine("Current state: " + state.GetType().Name);
}
}
public void Request()
{
state.Handle(this);
}
}
//Client
Context c = new Context(new ConcreateStateA());
c.Request();//每次请求实现状态切换
c.Request();
c.Request();
c.Request();

示例

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//不同时间点的工作状态示例

//抽象状态
public abstract class State
{
public abstract void WriteProgram(Work w);
}
//上午工作状态类
public class ShangWuState : State
{
public override void WriteProgram(Work w)
{
if(w.Hour < 12)
Console.WriteLine("上午工作状态");
else
{
w.SetState(new ZhongWUState())
w.WriteProgram();
}
}
}
public class ZhongWUState : State
{
public override void WriteProgram(Work w)
{
if(w.Hour < 13)
Console.WriteLine("中午工作状态");
else
{
w.SetState(new XiaWUState())
w.WriteProgram();
}
}
}
public class XiaWUState : State
{
public override void WriteProgram(Work w)
{
if(w.Hour < 17)
Console.WriteLine("下午工作状态");
else
{
w.SetState(new WanShangState())
w.WriteProgram();
}
}
}
public class WanShangState : State
{
public override void WriteProgram(Work w)
{
if(w.TaskFinished)
{
w.SetState(new RestState());
w.WriteProgram();
}
else
{
if(w.Hour < 21)
Console.WriteLine("晚上工作状态");
else
{
w.SetState(new SleepState())
w.WriteProgram();
}
}
}
}
public class SleepState : State
{
public override void WriteProgram(Work w)
{
Console.WriteLine("睡觉");
}
}
public class RestState : State
{
public override void WriteProgram(Work w)
{
Console.WriteLine("下班休息");
}
}

public class Work
{
private State current;
public Work()
{
current = new ShangWuState();//初始化为上午状态
}
private double hour;
public double Hour
{
get { return hour;}
set { hour = value;}
}
private bool finish = false;
public bool TaskFinish
{
get { return finish;}
set { finish = value;}
}
public void SetState(State s)
{
current = s;
}
public void WriteProgram()
{
current.WriteProgram(this);
}
}
//Client
Work work = new Work();
work.Hour = 9;
work.WriteProgram();
work.Hour = 12;
work.WriteProgram();
work.Hour = 16;
work.WriteProgram();
work.Hour = 20;
work.WriteProgram();