状态模式主要用于解决判断语句过多以至于当需求改变时需要在千丝万缕的判断中修改而发生的错误的问题。,但是缺点是每一个状态需要增加一个类,代码太多。
状态父类
package StatePattern;
public abstract class State {
public abstract void writeProgram(Work work);
}
状态子类
class MorningState extends State{
@Override
public void writeProgram(Work work) {
// TODO Auto-generated method stub
if (work.getHour()<12) {
System.out.println("当前时间"+work.getHour()+"上午工作很精神");
}else {
work.setCurrent(new NoonState());
work.writeProgram();
}
}
}
class NoonState extends State{
@Override
public void writeProgram(Work work) {
// TODO Auto-generated method stub
if (work.getHour()<14) {
System.out.println("当前时间"+work.getHour()+"午后犯困");
}else {
work.setCurrent(new AfternoonState());
work.writeProgram();
}
}
}
class AfternoonState extends State{
@Override
public void writeProgram(Work work) {
// TODO Auto-generated method stub
if (work.getHour()<17) {
System.out.println("当前时间"+work.getHour()+"下午状态还不错");
}else {
work.setCurrent(new EveningState());
work.writeProgram();
}
}
}
class EveningState extends State{
@Override
public void writeProgram(Work work) {
// TODO Auto-generated method stub
if (work.getFinish()) {
work.setCurrent(new RestState());
work.writeProgram();
}else {
if (work.getHour()<21) {
System.out.println("当前时间"+work.getHour()+"加班十分疲惫");
}else {
work.setCurrent(new SleepingState());
work.writeProgram();
}
}
}
}
class SleepingState extends State{
@Override
public void writeProgram(Work work) {
// TODO Auto-generated method stub
System.out.println("当前时间"+work.getHour()+"不行了,睡着了");
}
}
class RestState extends State{
@Override
public void writeProgram(Work work) {
// TODO Auto-generated method stub
System.out.println("当前时间"+work.getHour()+"下班回家咯");
}
}
工作类
package StatePattern;
public class Work {
private State current;//关联State,控制时间,工作完成状态,还有writeProgram()
private double hour;
private boolean finish = false;
public Work() {
// TODO Auto-generated constructor stub
current= new MorningState();
}
public double getHour() {
return hour;
}
public void setHour(double hour) {
this.hour = hour;
}
public boolean getFinish() {
return finish;
}
public void setFinish(boolean finish) {
this.finish = finish;
}
public State getCurrent() {
return current;
}
public void setCurrent(State current) {
this.current = current;
}
public void writeProgram(){
current.writeProgram(this);
}
}
界面类
package StatePattern;
public class Main {
public void printFrame(){
Work emergencyWork = new Work();//设置为早上状态
emergencyWork.setHour(9);//设置时间用于判断
emergencyWork.writeProgram();//间接调用当前状态的writeProgram,如果时间不符合,则重新设置状态并间接调用新状态的writeProgram,下面同理..
emergencyWork.setHour(10);
emergencyWork.writeProgram();
emergencyWork.setHour(12);
emergencyWork.writeProgram();
emergencyWork.setHour(13);
emergencyWork.writeProgram();
emergencyWork.setHour(14);
emergencyWork.writeProgram();
emergencyWork.setHour(17);
emergencyWork.writeProgram();
emergencyWork.setFinish(false);
emergencyWork.setHour(19);
emergencyWork.writeProgram();
emergencyWork.setHour(22);
emergencyWork.writeProgram();
}
public static void main(String[] args) {
Main main = new Main();
main.printFrame();
}
}
个人见解:虽然的确解决了if语句的问题,当时work类和state所有类有双向耦合的嫌疑,互相依赖,感觉有bad taste