一、概念
当一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为时,就可以考虑使用状态模式。
状态模式就是将特定状态相关的行为放入一个对象中,通过新增子类来增加新的状态和转换。通过把各种状态转移逻辑分布到state的子类之间,消除了庞大的条件分支语句。
二、代码示例
第一版
public class Work {
private Integer hour;
private Boolean isFinished;
// 方法过长是坏味道!
public void writeProgram() {
if (isFinished) {
System.out.println(hour + "点 回家休息");
} else {
if (hour >= 9 && hour < 12) {
System.out.println(hour + "点 上午工作");
} else if (hour >= 12 && hour < 13) {
System.out.println(hour + "点 中午吃饭");
} else if (hour >= 13 && hour < 17) {
System.out.println(hour + "点 下午工作");
} else {
System.out.println(hour + "点 加班中");
}
}
}
public Integer getHour() {
return hour;
}
public void setHour(Integer hour) {
this.hour = hour;
}
public Boolean getFinished() {
return isFinished;
}
public void setFinished(Boolean finished) {
isFinished = finished;
}
}
public class RunMain {
public static void main(String[] args) {
Work work = new Work();
work.setFinished(false);
work.setHour(9);
work.writeProgram();
work.setHour(12);
work.writeProgram();
work.setHour(15);
work.writeProgram();
work.setHour(18);
work.writeProgram();
work.setFinished(true);
work.setHour(21);
work.writeProgram();
}
}
第二版
public class Context {
private State state;
public Context(State state) {
this.state = state;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public void request() {
state.handle(this);
}
}
public abstract class State {
public abstract void handle(Context context);
}
public class StateA extends State {
@Override
public void handle(Context context) {
context.setState(new StateB());
}
}
public class StateB extends State {
@Override
public void handle(Context context) {
context.setState(new StateA());
}
}
public class RunMain {
public static void main(String[] args) {
Context context = new Context(new StateA());
// 不断请求同时改变状态
System.out.println(context.getState());
context.request();
System.out.println(context.getState());
context.request();
System.out.println(context.getState());
context.request();
System.out.println(context.getState());
context.request();
System.out.println(context.getState());
}
}
第三版
public class Work {
// 状态改变的依据
private Integer hour;
// 是否下班的依据
private Boolean isFinished;
// 工作状态抽象类
private State state;
public Work(State state) {
this.state = state;
}
public Integer getHour() {
return hour;
}
public void setHour(Integer hour) {
this.hour = hour;
}
public Boolean getFinished() {
return isFinished;
}
public void setFinished(Boolean finished) {
isFinished = finished;
}
public State getState() {
return state;
}
public void setState(State state) {
this.state = state;
}
public void writeProgram() {
state.writeProgram(this);
}
}
public abstract class State {
public abstract void writeProgram(Work work);
}
public class StateA extends State {
@Override
public void writeProgram(Work work) {
if (work.getHour() >= 9 && work.getHour() < 12) {
System.out.println(work.getHour() + "点 上午工作");
} else {
work.setState(new StateB());
work.writeProgram();
}
}
}
public class StateB extends State {
@Override
public void writeProgram(Work work) {
if (work.getHour() >= 13 && work.getHour() < 17) {
System.out.println(work.getHour() + "点 下午工作");
} else {
work.setState(new StateC());
work.writeProgram();
}
}
}
public class StateC extends State {
@Override
public void writeProgram(Work work) {
System.out.println(work.getHour() + "点 啥也不做");
}
}
public class RunMain {
public static void main(String[] args) {
// 给初始状态
Work work = new Work(new StateA());
work.setHour(9);
work.writeProgram();
work.setHour(15);
work.writeProgram();
work.setHour(21);
work.writeProgram();
}
}
三、状态模式&策略模式
状态模式是策略模式的孪生兄弟,是因为它们的UML图是一样的。但意图却完全不一样,策略模式是让用户指定更换的策略算法,而状态模式是状态在满足一定条件下的自动更换,用户无法指定状态,最多只能设置初始状态。
更多请看 https://www.runoob.com/w3cnote/state-vs-strategy.html