该项目源码地址:https://github.com/ggb2312/JavaNotes/tree/master/design-pattern(设计模式相关代码与笔记)
1. 定义
允许一个对象在其内部状态改变时,改变它的行为
2. 适用场景
一个对象存在多个状态(不同状态下行为不同),且状态可相互转换
比如:电商订单状态扭转等
3. 类图与角色
Context:它就是那个含有状态的对象,它可以处理一些请求,这些请求最终产生的响应会与状态相关。
State:状态接口,它定义了每一个状态的行为集合,这些行为会在Context中得以使用。
ConcreteState:具体状态,实现相关行为的具体状态类。
4. 相关设计模式
状态模式和享元模式
状态模式和享元模式可以组合使用
5. 模式实例
背景:视频的播放状态切换
(1)视频状态抽象类
/**
* Create by lastwhisper on 2019/2/11
*/
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();
}
(2)视频上下文
/**
* Create by lastwhisper on 2019/2/11
*/
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 pause() {
this.courseVideoState.pause();
}
public void stop() {
this.courseVideoState.stop();
}
}
(3)视频的各种具体状态类
PlayState 正常播放课程视频状态
/**
* Create by lastwhisper on 2019/2/11
*/
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);
}
}
SpeedState 快进播放课程视频状态
/**
* Create by lastwhisper on 2019/2/11
*/
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);
}
}
PauseState 暂停播放课程视频状态
/**
* Create by lastwhisper on 2019/2/11
*/
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);
}
}
StopState 停止播放课程视频状态
/**
* Create by lastwhisper on 2019/2/11
*/
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("停止播放课程视频状态");
}
}
(4)测试
/**
* Create by lastwhisper on 2019/2/11
*/
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();
}
}
测试结果:
(5)类图
6. 优缺点
优点:
- 将不同的状态隔离
- 把各种状态的转换逻辑,分布到State的子类中,减少相互间依赖
- 增加新的状态非常简单
缺点:
- 状态多的业务场景导致类数目增加,系统变复杂
7. 扩展-JDK1.7源码以及框架中的状态模式
javax.faces.lifecycle.Lifecycle、javax.faces.webapp.FacesServlet