赞
踩
State
,定义一个抽象方法,如 writeProgram
。ForenoonState
、NoonState
、AfternoonState
等,并重写抽象方法。Work
用来管理当前状态State
类public abstract class State {
public abstract void writeProgram(Work w);
}
ConcreteState
类:public class ForenoonState extends State { @Override public void writeProgram(Work w) { if(w.getHour() < 12) { System.out.println("当前时间:" + w.getHour() + "点 上午工作,精神百倍"); }else { w.setState(new NoonState()); w.writeProgram(); } } } public class NoonState extends State { @Override public void writeProgram(Work w) { if(w.getHour() < 13) { System.out.println("当前时间:" + w.getHour() + "点 饿了,午饭;犯困,午休"); }else { w.setState(new AfternoonState()); w.writeProgram(); } } } public class AfternoonState extends State { @Override public void writeProgram(Work w) { if(w.getHour() < 17){ System.out.println("
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。