Skip to content

Commit ae5bbca

Browse files
committed
状态模式
1 parent 1254557 commit ae5bbca

File tree

8 files changed

+279
-0
lines changed

8 files changed

+279
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* 适配器模式
3434
* 单例模式
3535
* 命令模式
36+
* 状态模式
3637

3738

3839

src/action/state/StateMain.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package action.state;/**
2+
* Created by MirsFang on 2017/3/16.
3+
*/
4+
5+
import action.state.absState.Context;
6+
import action.state.allState.Close;
7+
import action.state.allState.Run;
8+
9+
/***
10+
*作者:MirsFang
11+
*模式:状态模式
12+
*时间:2017/03/16/下午12:36
13+
*备注 执行类
14+
***/
15+
16+
public class StateMain {
17+
public static void main(String[] args) {
18+
Context context=new Context();
19+
context.setNowState(new Run());
20+
context.open();
21+
context.close();
22+
context.run();
23+
context.stop();
24+
}
25+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package action.state.absState;/**
2+
* Created by MirsFang on 2017/3/16.
3+
*/
4+
5+
import action.state.allState.Close;
6+
import action.state.allState.Open;
7+
import action.state.allState.Run;
8+
import action.state.allState.Stop;
9+
10+
/***
11+
*作者:MirsFang
12+
*模式:状态模式
13+
*时间:2017/03/16/下午12:14
14+
*备注 上下文环境
15+
***/
16+
17+
public class Context {
18+
19+
//定义出所有的电梯状态 用在setState();
20+
public static final Open open=new Open();
21+
public static final Close close=new Close();
22+
public static final Run run=new Run();
23+
public static final Stop stop=new Stop();
24+
25+
//当前的状态
26+
private State nowState;
27+
28+
public void setNowState(State nowState) {
29+
this.nowState = nowState;
30+
this.nowState.setContext(this);
31+
}
32+
33+
public State getNowState() {
34+
return nowState;
35+
}
36+
37+
public void open(){
38+
nowState.open();
39+
}
40+
41+
public void close(){
42+
nowState.close();
43+
}
44+
45+
public void run(){
46+
nowState.run();
47+
}
48+
49+
public void stop(){
50+
nowState.stop();
51+
}
52+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package action.state.absState;/**
2+
* Created by MirsFang on 2017/3/16.
3+
*/
4+
5+
/***
6+
*作者:MirsFang
7+
*模式:状态模式
8+
*时间:2017/03/16/下午12:13
9+
*备注 抽象状态角色
10+
***/
11+
12+
public abstract class State {
13+
protected Context context;
14+
15+
//设置状态
16+
public void setContext(Context context) {
17+
this.context = context;
18+
}
19+
20+
//电梯门开启的动作
21+
public abstract void open();
22+
//电梯门关闭的动作
23+
public abstract void close();
24+
//电梯运行起来
25+
public abstract void run();
26+
//电梯停下来
27+
public abstract void stop();
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package action.state.allState;/**
2+
* Created by MirsFang on 2017/3/16.
3+
*/
4+
5+
import action.state.absState.Context;
6+
import action.state.absState.State;
7+
8+
/***
9+
*作者:MirsFang
10+
*模式:状态模式
11+
*时间:2017/03/16/下午12:23
12+
*备注 关闭状态
13+
***/
14+
15+
public class Close extends State {
16+
17+
18+
@Override
19+
public void open() {
20+
//状态修改
21+
context.setNowState(Context.open);
22+
//委托执行
23+
context.getNowState().open();
24+
}
25+
26+
@Override
27+
public void close() {
28+
System.out.print("电梯门关闭");
29+
}
30+
31+
//运行
32+
@Override
33+
public void run() {
34+
//状态修改
35+
context.setNowState(Context.run);
36+
//委托执行
37+
context.getNowState().run();
38+
}
39+
40+
//停止
41+
@Override
42+
public void stop() {
43+
//状态修改
44+
context.setNowState(Context.stop);
45+
//委托执行
46+
context.getNowState().stop();
47+
48+
}
49+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package action.state.allState;/**
2+
* Created by MirsFang on 2017/3/16.
3+
*/
4+
5+
import action.state.absState.Context;
6+
import action.state.absState.State;
7+
8+
/***
9+
*作者:MirsFang
10+
*模式:状态模式
11+
*时间:2017/03/16/下午12:23
12+
*备注 打开状态
13+
***/
14+
15+
public class Open extends State {
16+
17+
18+
@Override
19+
public void open() {
20+
System.out.println("电梯门开启");
21+
}
22+
23+
@Override
24+
public void close() {
25+
//状态修改
26+
context.setNowState(Context.close);
27+
//委托执行
28+
context.getNowState().close();
29+
}
30+
31+
//开着门不能跑
32+
@Override
33+
public void run() {
34+
35+
}
36+
37+
//开着门就是停止的
38+
@Override
39+
public void stop() {
40+
41+
42+
}
43+
}

src/action/state/allState/Run.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package action.state.allState;/**
2+
* Created by MirsFang on 2017/3/16.
3+
*/
4+
5+
import action.state.absState.Context;
6+
import action.state.absState.State;
7+
8+
/***
9+
*作者:MirsFang
10+
*模式:状态模式
11+
*时间:2017/03/16/下午12:23
12+
*备注 运行状态
13+
***/
14+
15+
public class Run extends State {
16+
17+
18+
//运行状态不糊开门
19+
@Override
20+
public void open() {
21+
//状态修改
22+
}
23+
24+
//运行状态门就是观者的
25+
@Override
26+
public void close() {
27+
28+
}
29+
30+
@Override
31+
public void run() {
32+
System.out.println("电梯正在运行");
33+
}
34+
35+
//停止运行
36+
@Override
37+
public void stop() {
38+
context.setNowState(Context.stop);
39+
context.getNowState().stop();
40+
}
41+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package action.state.allState;/**
2+
* Created by MirsFang on 2017/3/16.
3+
*/
4+
5+
import action.state.absState.Context;
6+
import action.state.absState.State;
7+
8+
/***
9+
*作者:MirsFang
10+
*模式:状态模式
11+
*时间:2017/03/16/下午12:23
12+
*备注 停止状态
13+
***/
14+
15+
public class Stop extends State {
16+
17+
//停下来要开电梯门
18+
@Override
19+
public void open() {
20+
context.setNowState(Context.open);
21+
context.getNowState().open();
22+
}
23+
24+
25+
@Override
26+
public void close() {
27+
28+
}
29+
30+
@Override
31+
public void run() {
32+
context.setNowState(Context.run);
33+
context.getNowState().run();
34+
}
35+
36+
@Override
37+
public void stop() {
38+
System.out.println("电梯停止了");
39+
}
40+
}

0 commit comments

Comments
 (0)