状态模式(State),当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。代替大量if else if判断分支条件。
PS:其实就通过子类的各种继承来进行逻辑转换,通过定义新的子类可以很容易地增加新的状态和转换。
当一个对象的行为取决于它的状态,并且它必须在运行时刻根据状态改变它的行为时,就可以考虑使用状态模式
主方法
public class main {
public static void main(String[] args) {
Work emergencyProjects = new Work();
emergencyProjects.setHour(9);
emergencyProjects.writeProgram();
emergencyProjects.setHour(10);
emergencyProjects.writeProgram();
emergencyProjects.setHour(12);
emergencyProjects.writeProgram();
emergencyProjects.setHour(13);
emergencyProjects.writeProgram();
emergencyProjects.setHour(14);
emergencyProjects.writeProgram();
emergencyProjects.setHour(17);
emergencyProjects.setFinish(true);
// emergencyProjects.setFinish(false);
emergencyProjects.writeProgram();
emergencyProjects.setHour(19);
emergencyProjects.writeProgram();
emergencyProjects.setHour(22);
emergencyProjects.writeProgram();
}
}
状态抽象类
/**
* 抽象状态类
*/
public abstract class State {
public abstract void writeProgram(Work work);
}
工作类
/**
* 工作类
*/
public class Work {
public Work() {
current = new ForenoonState();
}
private State current;
private int hour;//时间属性,状态转换的依据
private boolean finish = false;//工作任务是否完成的标志位
public void writeProgram() {
current.writeProgram(this);
}
public State getCurrent() {
return current;
}
public void setCurrent(State current) {
this.current = current;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public boolean isFinish() {
return finish;
}
public void setFinish(boolean finish) {
this.finish = finish;
}
}
多种状态类
public class ForenoonState extends State {
public void writeProgram(Work work) {
if (work.getHour() < 12) {
System.out.println(String.format("当前时间:{%d}点 上午工作,精神百倍", work.getHour()));
} else {
work.setCurrent(new NoonState());
work.writeProgram();
}
}
}
public class NoonState extends State {
public void writeProgram(Work work) {
if (work.getHour() < 13) {
System.out.println(String.format("当前时间:{%d}点 饿了,午饭;犯困,午休。", work.getHour()));
} else {
work.setCurrent(new AfternoonState());
work.writeProgram();
}
}
}
public class AfternoonState extends State {
public void writeProgram(Work work) {
if (work.getHour() < 17) {
System.out.println(String.format("当前时间:{%d}点 下午状态还不错,继续努力", work.getHour()));
} else {
work.setCurrent(new EveningState());
work.writeProgram();
}
}
}
/**
* 加个finish判断,如果任务完成即下班
*/
public class EveningState extends State {
public void writeProgram(Work work) {
if (work.isFinish()) {//如果工作完成了就调用下班休息类
work.setCurrent(new RestState());
work.writeProgram();
} else {
if (work.getHour() < 21) {
System.out.println(String.format("当前时间:{%d}点 加班哦,疲累之极", work.getHour()));
} else {
work.setCurrent(new SleepingState());//工作未完成
work.writeProgram();
}
}
}
}
public class RestState extends State {
public void writeProgram(Work work) {
System.out.println(String.format("当前时间:{%d}点 下班回家了", work.getHour()));
}
}
public class SleepingState extends State {
public void writeProgram(Work work) {
System.out.println(String.format("当前时间:{%d}点 不行了,睡着了", work.getHour()));
}
}