1 概念
1 定义
当一个对象内在状态改变时允许改变其行为。比如:停止状态下,不允许快进视频这个行为
类型:行为型
核心角色
- 环境类(Context):环境类是拥有状态的对象。它维护一个指向当前状态对象的引用,并将与状态相关的请求委托给状态对象进行处理。
- 抽象状态类(State):抽象状态类定义了一个接口,用于封装与环境对象相关的行为。它可以根据具体的状态进行具体的行为实现。
- 具体状态类(Concrete State):具体状态类实现了抽象状态类中定义的接口,提供了与具体状态相关的行为实现。
2 应用场景
一个对象存在多个状态(不同状态下行为不同),且状态可以转换
3 优缺点
优点:
- 将不同状态隔离
- 将各种状态的转换逻辑,分布到state的子类中,减少互相依赖
- 增加新的状态简单
缺点:
2 代码实现
场景:网站的课程视频有暂停,快进,停止,播放状态
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 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); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| 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); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| 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("停止播放课程视频状态"); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
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 PauseState PAUSE_STATE = new PauseState(); public final static SpeedState SPEED_STATE = new SpeedState();
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 stop(){ this.courseVideoState.stop(); }
public void pause(){ this.courseVideoState.pause(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| 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 停止状态不能快进!!