Skip to content

Commit 885150b

Browse files
committed
initialize
1 parent 7bb89e7 commit 885150b

File tree

3 files changed

+222
-0
lines changed

3 files changed

+222
-0
lines changed

State/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
### 简介
2+
状态模式允许对象在其内部状态发生变化时,改变其行为。
3+
比如,空调在*关机*状态时的行为,只能是*开机*;而在*开机*状态时的行为,可以是制冷,制热,关闭等。
4+
5+
---
6+
7+
### 角色
8+
9+
* Context
10+
环境角色,也称为上下文角色。它定义了客户端*感兴趣*的接口,同时会在内部保存一个具体状态对象的引用。这个具体状态对象代表环境对象当前所处的状态
11+
* State
12+
抽象状态角色。它为所有的具体状态类定义了公共的接口
13+
* ConcreteState
14+
继承或实现抽象状态角色。一个具体状态类定义了**一组特定的行为**
15+
16+
---
17+
18+
### UML类图
19+
20+
![state.png](http://timd.cn/content/images/pictures/state.png)
21+

State/StatePattern.java

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
class Context {
2+
private final AirConditioningState open =
3+
new OpenState();
4+
private final AirConditioningState close =
5+
new CloseState();
6+
private final AirConditioningState cool =
7+
new CoolState();
8+
private final AirConditioningState heat =
9+
new HeatState();
10+
private AirConditioningState currentState = close;
11+
12+
public void toOpen() {
13+
if (currentState.open())
14+
currentState = open;
15+
}
16+
17+
public void toClose() {
18+
if (currentState.close())
19+
currentState = close;
20+
}
21+
22+
public void toCool() {
23+
if (currentState.cool())
24+
currentState = cool;
25+
}
26+
27+
public void toHeat() {
28+
if (currentState.heat())
29+
currentState = heat;
30+
}
31+
}
32+
33+
abstract class AirConditioningState {
34+
public boolean open() {
35+
System.out.println("illegal state, unable to open");
36+
return false;
37+
}
38+
39+
public boolean close() {
40+
System.out.println("illegal state, unable to close");
41+
return false;
42+
}
43+
44+
public boolean cool() {
45+
System.out.println("illegal state, unable to cool");
46+
return false;
47+
}
48+
49+
public boolean heat() {
50+
System.out.println("illegal state, unable to heat");
51+
return false;
52+
}
53+
}
54+
55+
class OpenState extends AirConditioningState {
56+
public boolean close() {
57+
System.out.println("change to state: `close`");
58+
return true;
59+
}
60+
61+
public boolean cool() {
62+
System.out.println("change to state: `cool`");
63+
return true;
64+
}
65+
66+
public boolean heat() {
67+
System.out.println("change to state: `heat`");
68+
return true;
69+
}
70+
}
71+
72+
class CloseState extends AirConditioningState {
73+
public boolean open() {
74+
System.out.println("change to state: `open`");
75+
return true;
76+
}
77+
}
78+
79+
class CoolState extends AirConditioningState {
80+
public boolean close() {
81+
System.out.println("change to state: `close`");
82+
return true;
83+
}
84+
85+
public boolean heat() {
86+
System.out.println("change to state: `heat`");
87+
return true;
88+
}
89+
}
90+
91+
class HeatState extends AirConditioningState {
92+
public boolean close() {
93+
System.out.println("change to state: `close`");
94+
return true;
95+
}
96+
97+
public boolean cool() {
98+
System.out.println("change to state: `cool`");
99+
return true;
100+
}
101+
}
102+
103+
104+
public class StatePattern {
105+
public static void main(String[] args) {
106+
Context context = new Context();
107+
context.toHeat();
108+
context.toOpen();
109+
context.toHeat();
110+
context.toCool();
111+
context.toClose();
112+
context.toClose();
113+
}
114+
}
115+

State/StatePattern.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
class Context:
2+
def __init__(self):
3+
self._open = OpenState()
4+
self._close = CloseState()
5+
self._cool = CoolState()
6+
self._heat = HeatState()
7+
self._current_state = self._close;
8+
9+
def to_open(self):
10+
if self._current_state.open():
11+
self._current_state = self._open
12+
13+
def to_close(self):
14+
if self._current_state.close():
15+
self._current_state = self._close
16+
17+
def to_cool(self):
18+
if self._current_state.cool():
19+
self._current_state = self._cool
20+
21+
def to_heat(self):
22+
if self._current_state.heat():
23+
self._current_state = self._heat
24+
25+
class AirContiditoningState:
26+
def open(self):
27+
print("illegal state, unable to open")
28+
return False
29+
30+
def close(self):
31+
print("illegal state, unable to close")
32+
return False
33+
34+
def cool(self):
35+
print("illegal state, unable to cool")
36+
return False
37+
38+
def heat(self):
39+
print("illegal state, unable to heat")
40+
return False
41+
42+
class OpenState(AirContiditoningState):
43+
def close(self):
44+
print("change to state: `close`")
45+
return True
46+
47+
def cool(self):
48+
print("change to state: `cool`")
49+
return True
50+
51+
def heat(self):
52+
print("change to state: `heat`")
53+
return True
54+
55+
class CloseState(AirContiditoningState):
56+
def open(self):
57+
print("change to state: `open`")
58+
return True
59+
60+
class CoolState(AirContiditoningState):
61+
def close(self):
62+
print("change to state: `close`")
63+
return True
64+
65+
def heat(self):
66+
print("change to state: `heat`")
67+
return True
68+
69+
class HeatState(AirContiditoningState):
70+
def close(self):
71+
print("change to state: `close`")
72+
return True
73+
74+
def cool(self):
75+
print("change to state: `cool`")
76+
return True
77+
78+
if __name__ == "__main__":
79+
context = Context()
80+
context.to_heat();
81+
context.to_open();
82+
context.to_heat();
83+
context.to_cool();
84+
context.to_close();
85+
context.to_close();
86+

0 commit comments

Comments
 (0)