/** * 狀態上下文 * 維護一個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