实例是从《设计模式之禅》这本书中得来的。
三个构成:抽象状态类AState、状态实例、上下文信息context。
以电梯为例,状态为:open、close、run、stop。对应电梯相应的open、close、run、stop行为。
Astate类如下:
/** * */ package test20150507_designPatterns.statePattern_20150811; /** * @author User *此处以设计模式之禅中的电梯状态为例 *此处为抽象的电梯状态类 *共有四个行为:open() close() run() stop() *和四个状态,每个状态都有前置行为或后置行为。只有满足条件才可以进行行为的操作 */ public abstract class AState { protected Context context; public void setContext(Context context){ this.context=context; } abstract void open(); abstract void close(); abstract void run(); abstract void stop(); }
AState的实现类如下:
OpenState:
/** * */ package test20150507_designPatterns.statePattern_20150811; /** * @author User * */ public class OpenState extends AState{ void open() { System.out.println("电梯打开"); } void close() { super.context.setAState(super.context.CLOSE); super.context.close(); } void run() { //do nothing } void stop() { //do nothing } }
CloseState:
/** * */ package test20150507_designPatterns.statePattern_20150811; /** * @author User * */ public class CloseState extends AState{ void open() { super.context.setAState(super.context.OPEN); super.context.open(); } void close() { //执行关闭的操作 System.out.println("关闭电梯"); } void run() { super.context.setAState(super.context.RUN); super.context.run(); } void stop() { super.context.setAState(super.context.STOP); super.context.stop(); } }
RunState:
/** * */ package test20150507_designPatterns.statePattern_20150811; /** * @author User * */ public class RunState extends AState{ void open() { //do nothing } void close() { // 本来就是关闭状态 } void run() { System.out.println("电梯运行中!!!"); } void stop() { super.context.setAState(super.context.STOP); super.context.stop(); } }
StopState:
/** * */ package test20150507_designPatterns.statePattern_20150811; /** * @author User * */ public class StopState extends AState{ void open() { super.context.setAState(super.context.OPEN); super.context.open(); } void close() { super.context.setAState(super.context.CLOSE); super.context.close(); } void run() { super.context.setAState(super.context.RUN); super.context.run(); } void stop() { System.out.println("电梯停止运行"); } }
上下文信息Context类如下:
/** * */ package test20150507_designPatterns.statePattern_20150811; /** * @author User * */ public class Context { public static final AState OPEN=new OpenState(); public static final AState CLOSE=new CloseState(); public static final AState RUN=new RunState(); public static final AState STOP=new StopState(); private AState curState; public void setAState(AState state){ this.curState=state; this.curState.setContext(this); } public void open(){ this.curState.open(); } public void close(){ this.curState.close(); } public void run(){ this.curState.run(); } public void stop(){ this.curState.stop(); } }
测试类如下:
/** * */ package test20150507_designPatterns.statePattern_20150811; /** * @author User *场景类 */ public class Client { public static void main(String[] args) { Context context=new Context(); context.setAState(context.STOP); //设置初始状态是停止状态 context.open(); System.out.println("人-----------------进出"); context.close(); context.run(); context.stop(); } }
运行结果:
电梯打开 人-----------------进出 关闭电梯 电梯运行中!!! 电梯停止运行