状态模式:当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。
简单描述:一个Context类(存有一个抽象状态State引用),调用状态类的方法。State的具体类方法中会判断Context类的状态(如时间),满足一个状态则执行相应动作,否则把Context的State引用指向下一个状态类,由下一个状态类决定相应行为
大话设计模式中的截图:
例子代码:
Work类(Context):
1 package com.longsheng.state;
2
3 public class Work {
4
5 private int hour = 0;
6 private boolean finish = false;
7 AbstractState mCurrentState = null;
8
9 public int getHour() {
10 return hour;
11 }
12
13 public void setHour(int hour) {
14 this.hour = hour;
15 }
16
17 public boolean isFinish() {
18 return finish;
19 }
20
21 public void setFinish(boolean isFinish) {
22 this.finish = isFinish;
23 }
24
25 public Work() {
26 mCurrentState = new ForenooonState();
27 }
28
29 public void writeProgram() {
30 mCurrentState.writeProgram(this);
31 }
32
33 public void setState( AbstractState mAbstractState ) {
34 this.mCurrentState = mAbstractState;
35 }
36
37 }
AbstractState类:
1 package com.longsheng.state;
2
3 public abstract class AbstractState {
4
5 public abstract void writeProgram(Work mWork);
6
7 }
ForenooonState类:
1 package com.longsheng.state;
2
3 public class ForenooonState extends AbstractState {
4
5 @Override
6 public void writeProgram(Work mWork) {
7 if( mWork.getHour() < 12 ) {
8 System.out.println("当前时间:" + mWork.getHour() + "点,上午工作,精神百倍");
9 } else {
10 mWork.setState(new NoonState());
11 mWork.writeProgram();
12 }
13 }
14
15 }
NoonState类:
1 package com.longsheng.state;
2
3 public class NoonState extends AbstractState {
4
5 @Override
6 public void writeProgram(Work mWork) {
7 if( mWork.getHour() < 13 ) {
8 System.out.println("当前时间:" + mWork.getHour() + "点,中午时间,吃饭休息");
9 } else {
10 mWork.setState(new AfternoonState());
11 mWork.writeProgram();
12 }
13 }
14
15 }
AfternoonState类:
1 package com.longsheng.state;
2
3 public class AfternoonState extends AbstractState {
4
5 @Override
6 public void writeProgram(Work mWork) {
7 if( mWork.getHour() < 17 ) {
8 System.out.println("当前时间:" + mWork.getHour() + "点,下午状态还不错,继续努力");
9 } else {
10 mWork.setState(new EveningState());
11 mWork.writeProgram();
12 }
13 }
14
15 }
EveningState类:
1 package com.longsheng.state;
2
3 public class EveningState extends AbstractState {
4
5 @Override
6 public void writeProgram(Work mWork) {
7 if (mWork.isFinish()) {
8 mWork.setState(new RestState());
9 mWork.writeProgram();
10 } else {
11 if( mWork.getHour() < 21 ) {
12 System.out.println("当前时间:" + mWork.getHour() + "点,又加班了,继续努力吧");
13 } else {
14 mWork.setState(new SleepingState());
15 mWork.writeProgram();
16 }
17 }
18 }
19
20 }
RestState类:
1 package com.longsheng.state;
2
3 public class RestState extends AbstractState {
4
5 @Override
6 public void writeProgram(Work mWork) {
7 System.out.println("当前时间:" + mWork.getHour() + "点,终于完成了,下班");
8 }
9
10 }
SleepingState类:
1 package com.longsheng.state;
2
3 public class SleepingState extends AbstractState {
4
5 @Override
6 public void writeProgram(Work mWork) {
7 System.out.println("当前时间:" + mWork.getHour() + "点,头疼,睡一会再继续努力");
8 }
9
10 }
客户端类:
1 package com.longsheng.state;
2
3 public class Client {
4
5 public static void main(String[] args) {
6 Work mWork = new Work();
7 mWork.setHour(9);
8 mWork.writeProgram();
9
10 mWork.setHour(10);
11 mWork.writeProgram();
12
13 mWork.setHour(11);
14 mWork.writeProgram();
15
16 mWork.setHour(12);
17 mWork.writeProgram();
18
19 mWork.setHour(13);
20 mWork.writeProgram();
21
22 mWork.setHour(15);
23 mWork.writeProgram();
24
25 mWork.setHour(17);
26 mWork.writeProgram();
27
28 mWork = new Work();//新建对象
29 mWork.setHour(20);
30 mWork.writeProgram();
31
32 mWork.setFinish(true);
33 //mWork.setFinish(false);
34
35 mWork.setHour(22);
36 mWork.writeProgram();
37
38 }
39
40 }
运行结果:
1 当前时间:9点,上午工作,精神百倍
2 当前时间:10点,上午工作,精神百倍
3 当前时间:11点,上午工作,精神百倍
4 当前时间:12点,中午时间,吃饭休息
5 当前时间:13点,下午状态还不错,继续努力
6 当前时间:15点,下午状态还不错,继续努力
7 当前时间:17点,又加班了,继续努力吧
8 当前时间:20点,又加班了,继续努力吧
9 当前时间:22点,终于完成了,下班
状态模式主要解决的是当控制一个对象状态转换的条件表达式过于复杂时的情况。把状态封判断逻辑转移到表示不同状态的一系列类当中,可以把复杂的判断逻辑简化。
将特定的状态相关的行为都放入一个对象中,由于所有与状态相关的代码都存在于某个ConcreteState中,所以通过定义新的子类可以很容易地增加新的状态和转换。
使用场景:当一个对象的行为取决于它的状态,并且它必须在运行时根据状态改变它的行为时,就可以考虑使用状态模式了
大话设计模式_状态模式(Java代码),布布扣,bubuko.com
时间: 2024-10-07 09:07:43