toc
#状态模式
当一个对象的内在状态改变时允许改变其行为,这个对象看起来像是改变了其类。
在平常开发自己也会遇到好多方法过长,里面的判断语句太多,导致后续修改十分麻烦。今天看到状态模式的介绍,觉得很受益。下面通过一段代码错误的显示一遍没有使用状态模式的代码
代码语言:txt复制public class Work {
private int hour;
private boolean finished;
private boolean taskFinished;
public void workProgram() {
if (hour < 12) {
System.out.println("current time: " new Date() " working in the morning");
} else if (hour < 13) {
System.out.println("current time: " new Date() " have a break!");
} else if (hour < 17) {
System.out.println("current time: " new Date() " working in the afternoon");
} else {
if (finished) {
System.out.println("current time: " new Date() " after work!");
} else if (hour <12) {
System.out.println("current time: " new Date() " very hard!");
}
}
}
}
从上述代码中看出,当我要修改hour的判断范围,或者在各个判断条件之后新增一些功能,都会使这段方法变得十分长,而且阅读起来也比较繁琐。
所以代码过长了就有坏味道了。在面向对象的设计中也希望做到代码的责任分解。
当一个对象状态转换的条件表达式过于复杂的时候,把状态的判断逻辑移到不同的一系列类当中,可以把复杂的判断逻辑简单化,这就是状态模式的主要作用。但并不是所有有判断的都要使用状态模式,简单的判断逻辑就不需要了。
代码改良
代码语言:txt复制/**
* 工作类 没有了判断语句
*/
public class Work {
private int hour;
private boolean finished;
private boolean taskFinished;
private State state;
private State currentState;
public Work() {
currentState = new ForenoonState();
}
public void workProgram() {
currentState.workProgram(this);
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public boolean isFinished() {
return finished;
}
public void setFinished(boolean finished) {
this.finished = finished;
}
public boolean isTaskFinished() {
return taskFinished;
}
public void setTaskFinished(boolean taskFinished) {
this.taskFinished = taskFinished;
}
public State getState() {
return state;
}
public void setState(State state) {
this.currentState = state;
}
}
/**
* morning
*/
public class ForenoonState extends State {
public void workProgram(Work work) {
if (work.getHour() < 12) {
System.out.println("current time: " new Date() " working in the morning");
} else {
work.setState(new NoonState());
}
}
}
/**
* noon
*/
public class NoonState extends State {
public void workProgram(Work work) {
if (work.getHour() < 13) {
System.out.println("current time: " new Date() " have a break!");
} else {
work.setState(new AfternoonState());
}
}
}
/**
* afternoon
*/
public class AfternoonState extends State {
public void workProgram(Work work) {
if (work.getHour() < 17) {
System.out.println("current time: " new Date() " working in the afternoon");
} else {
//todo .......
}
}
}
/**
* Client
*/
public class Client {
public static void main(String[] args) {
Work work = new Work();
work.setHour(12);
work.workProgram();
work.setHour(13);
work.workProgram();
}
}
从上术代码中,在客户端代码不需要做任何的修改,程序可以变得非常灵活,当程序还需要添加一个额外的工作状态时,再添加一个其它状态类的代码,也不影响其它状态类的代码。