-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContext.java
More file actions
38 lines (31 loc) · 820 Bytes
/
Copy pathContext.java
File metadata and controls
38 lines (31 loc) · 820 Bytes
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
package JavaDesignModel;
/**
* @author hanshizhe
* 状态模式,切换状态,类似与qq的隐身,在线状态。来进行切换
*/
public class Context {
private State state=null;
public Context(State state) {
super();
this.state = state;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public void method(){
if("state1".equals(state.getState())){
state.method1();
}else if("state2".equals(state.getState())){
state.method2();
}
}
public static void main(String[] args) {
State state = new State();
state.setState("state1");
Context context = new Context(state);
context.method();
}
}