Allow an object to alter its behavior when its internal state changes.The object will appear to change its class.(当一个对象内在状态改变时,允许其改变行为,这个对象看起来像改变了其类)
状态模式适应
public abstract class LiftState { protected Context context; public void setContext(Context _context){ this.context=_context; } public abstract void open(); public abstract void close(); public abstract void run(); public abstract void stop(); }关闭状态
public class ClosingState extends LiftState { @Override public void close() { System.out.println("close the lift"); } @Override public void open() { super.context.setLiftState(Context.openningState); super.context.getLiftState().open(); } @Override public void run() { super.context.setLiftState(Context.runningState); super.context.getLiftState().run(); } @Override public void stop() { super.context.setLiftState(Context.stoppingState); super.context.getLiftState().stop(); } }停止状态
public class StoppingState extends LiftState { @Override public void close() { //do nothing } @Override public void open() { super.context.setLiftState(Context.openningState); super.context.getLiftState().open(); } @Override public void run() { super.context.setLiftState(Context.runningState); super.context.getLiftState().run(); } @Override public void stop() { System.out.println("the lift is stoped"); } }运行状态
public class RunningState extends LiftState { @Override public void close() { //do nothing } @Override public void open() { //do nothing } @Override public void run() { System.out.println("lift is runing..."); } @Override public void stop() { super.context.setLiftState(Context.stoppingState); super.context.getLiftState().stop(); } }开门状态
public class OpenningState extends LiftState { @Override public void close() { super.context.setLiftState(Context.closingState); //调用的是Context中的方法 super.context.getLiftState().close(); //通过Context调用另一个状态的方法 } @Override public void open() { System.out.println("lift is open..."); } @Override public void run() { //do nothing } @Override public void stop() { //do nothing } }上下文对象
public class Context { public final static OpenningState openningState=new OpenningState(); public final static ClosingState closingState=new ClosingState(); public final static RunningState runningState=new RunningState(); public final static StoppingState stoppingState=new StoppingState(); private LiftState liftState; public LiftState getLiftState() { return liftState; } public void setLiftState(LiftState liftState) { this.liftState = liftState; this.liftState.setContext(this); //将该状态设置上下文对象 } public void close() { this.liftState.close(); } public void open() { this.liftState.open(); } public void run() { this.liftState.run(); } public void stop() { this.liftState.stop(); } }
public class Client { public static void main(String[] args) { Context context=new Context(); context.setLiftState(Context.closingState); context.open(); context.close(); context.run(); context.stop(); } } /* 上下文对象Context,成员属性LiftState,可设置四种状态,获取和改变状态,相当于一个公共容器 四种状态的抽象LiftState,成员属性Context,可设置上下文对象,让四种状态都拥有同一个Context 你中有我,我中有你,通过容器Context可以进行状态的切换,将四者联合成一体 当状态改变时,通过父类的统一Context改变了LiftState状态,然后调用了改变后状态的方法 也就是说 */
lift is open... close the lift lift is runing... the lift is stoped