1 代码演练
1.1 代码演练1
1 代码演练
1.1 代码演练1
需求:
课程视频有播放,快进,暂停,停止(关闭)四种状态,状态之间可以相互切换,但是停止状态不能切换到快进 和暂停状态
重点:(个人开发中没有注意到的地方)
1 核心:上下文类:this.courseVideoState.setCourseVideoContext(this); 相当于把State 直接把 当前本身状态 直接放到 上下文(CourseVideoContext)中
2 重点:状态基类:因为要被子类所使用的属性,所以权限设置成protected
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