/** * 状态上下文 * 维护一个state实例,这个为实体当前的状态 */ public class Context { /** * 当前的状态 */ private State state; /** * 构造函数 * @param state */ public Context(State state) { this.state = state; } /** * 请求状态 */ public void request() { state.handler(this); } //--set get public State getState() { return state; } public void setState(State state) { this.state = state; } } /** * 状态接口 */ public interface State { void handler(Context context); } /** * 具体的状态 */ public class ConcreteStateA implements State { @Override public void handler(Context context) { System.out.println("this is state A."); context.setState(new ConcreteStateB()); } } /** * 具体的状态 */ public class ConcreteStateB implements State { @Override public void handler(Context context) { System.out.println("this is state B."); context.setState(new ConcreteStateA()); } }
/** * 测试与应用 */ public class Test { public static void main(String[] args) { //建立状态上下文 Context context = new Context(new ConcreteStateA()); //切换状态, 这个有点相似电灯的开关状态 context.request(); context.request(); context.request(); context.request(); context.request(); } }
this is state A. this is state B. this is state A. this is state B. this is state A.
状态模式中的角色介绍html
假设一个视频的播放有播放中、暂停、快进和中止等等状态,使用状态模式实现这个功能。
/** * 课程状态上下文 */ public class CourseVideoContext { //当前状态 private CourseVideoState courseVideoState; //播放 public final static PlayState PLAY_STATE = new PlayState(); //中止 public final static StopState STOP_STATE = new StopState(); //快进 public final static SpeedState SPEED_STATE = new SpeedState(); //暂停 public final static PauseState PAUSE_STATE = new PauseState(); public CourseVideoState getCourseVideoState() { return courseVideoState; } public void setCourseVideoState(CourseVideoState courseVideoState) { this.courseVideoState = courseVideoState; this.courseVideoState.setCourseVideoContext(this); } public void play() { this.courseVideoState.play(); } public void speed() { this.courseVideoState.speed(); } public void pause() { this.courseVideoState.pause(); } public void stop() { this.courseVideoState.stop(); } } /** * 状态 */ public abstract class CourseVideoState { protected CourseVideoContext courseVideoContext; public void setCourseVideoContext(CourseVideoContext courseVideoContext) { this.courseVideoContext = courseVideoContext; } public abstract void play(); public abstract void speed(); public abstract void pause(); public abstract void stop(); } /** * 暂停状态 */ public class PauseState extends CourseVideoState { @Override public void play() { super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE); } @Override public void speed() { super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE); } @Override public void pause() { System.out.println("暂停播放视频"); } @Override public void stop() { super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE); } } /** * 播放状态 */ public class PlayState extends CourseVideoState { @Override public void play() { System.out.println("正常播放视频的状态"); } @Override public void speed() { super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE); } @Override public void pause() { super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE); } @Override public void stop() { super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE); } } /** * 加速状态 */ public class SpeedState extends CourseVideoState { @Override public void play() { super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE); } @Override public void speed() { System.out.println("快进播放视频"); } @Override public void pause() { super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE); } @Override public void stop() { super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE); } } /** * 中止状态 */ public class StopState extends CourseVideoState { @Override public void play() { super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE); } @Override public void speed() { System.out.println("ERROR 中止状态不能快进!!!"); } @Override public void pause() { System.out.println("ERROR 中止状态不能暂停!!!"); } @Override public void stop() { System.out.println("中止播放视频。"); } }
/** * 测试与应用 */ public class Test { public static void main(String[] args) { //状态上下文 CourseVideoContext courseVideoContext = new CourseVideoContext(); //状态 courseVideoContext.setCourseVideoState(new PlayState()); System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName()); courseVideoContext.pause(); System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName()); courseVideoContext.speed(); System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName()); courseVideoContext.stop(); System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName()); courseVideoContext.speed(); } }
当前状态:PlayState 当前状态:PauseState 当前状态:SpeedState 当前状态:StopState ERROR 中止状态不能快进!!!
慕课网设计模式精讲
:https://coding.imooc.com/class/270.html 设计模式学习笔记-状态模式
: https://www.cnblogs.com/wangjq/archive/2012/07/16/2593485.html