1コードのチュートリアル
1.1コードチュートリアル1
1コードのチュートリアル
1.1コードチュートリアル1
要件:
そこ早送りコースビデオの再生、一時停止、停止、あなたがそれぞれの状態を切り替えることができます(クローズ)4つの状態が、ですが、状態は早送りや一時停止状態を停止するために切り替えることはできません
:(個人的な開発の焦点は)場所に気付きませんでした
コア1:コンテキストクラス:this.courseVideoState.setCourseVideoContext(この);直接コンテキスト(CourseVideoContext)中に自身の現在の状態への直接等価
キー2:ステータス基本クラス:サブクラスのプロパティを使用することがあるため、提供それは保護されて権限
uml类图:
テストカテゴリ:
package com.geely.design.pattern.behavioral.state; public class Test { @org.junit.Test public void testVideoState(){ 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(); System.out.println("目前状态为:"+courseVideoContext.getCourseVideoState().getClass().getSimpleName()); } }
视频状态基类:
package com.geely.design.pattern.behavioral.state; public abstract class CourseVideoState { //重点:因为要被子类所使用的属性,所以权限设置成protected 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(); }
上下文类:
package com.geely.design.pattern.behavioral.state; public class CourseVideoContext { private CourseVideoState courseVideoState; public final static PlayState PLAY_STATE = new PlayState(); public final static PauseState PAUSE_STATE = new PauseState(); public final static SpeedState SPEED_STATE = new SpeedState(); public final static StopState STOP_STATE = new StopState(); //注意这里是get方法,不是构造器 public CourseVideoState getCourseVideoState() { return courseVideoState; } /** * 核心:重点:this.courseVideoState.setCourseVideoContext(this); 相当于把State 直接把 当前本身状态 直接放到 上下文(CourseVideoContext)中 * @param courseVideoState */ public void setCourseVideoState(CourseVideoState courseVideoState) { this.courseVideoState = courseVideoState; this.courseVideoState.setCourseVideoContext(this); } public void Play(){ this.courseVideoState.play(); } public void Pause(){ this.courseVideoState.pause(); } public void Speed(){ this.courseVideoState.speed(); } public void Stop(){ this.courseVideoState.stop(); } }
播放状态类:
package com.geely.design.pattern.behavioral.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); } }
暂停状态类:
package com.geely.design.pattern.behavioral.state; 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); } }
快进状态类:
package com.geely.design.pattern.behavioral.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); } }
停止状态类:
package com.geely.design.pattern.behavioral.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("已关闭"); } }
打印日志:
目前状态为:PlayState
目前状态为:PauseState
目前状态为:SpeedState
目前状态为:StopState
ERROR 关闭状态 不能切换到 快进状态
目前状态为:StopState
Process finished with exit code 0