状态模式
一、状态模式介绍
1、定义与类型
允许一个对象在其内部状态改变时,改变它的行为
类型:行为型
2、适用场景
一个对象存在多个状态(不同状态下行为不同),且状态可相互转换
3、优点
将不同的状态隔离
把各种状态的转换逻辑,分布到State的子类中,减少相互间依赖
增加新的状态非常简单
4、缺点
状态多的业务场景导致类数目增加,系统变复杂
5、相关设计模式
状态模式和享元模式 有时候可以配合进行使用
二、代码示例
模拟场景:看视频时,可以切换为播放状态、暂停状态、快进状态、停止状态
视频状态抽象类:
public abstract class VideoVideoState {
// 与上下文 互相嵌套
protected VideoStateContext videoStateContext;
public void setVideoStateContext(VideoStateContext videoStateContext) {
this.videoStateContext = videoStateContext;
}
public abstract void paly();
public abstract void speed();
public abstract void pause();
public abstract void stop();
}
播放状态类:
public class PlayState extends VideoVideoState {
@Override
public void paly() {
System.out.println("正常播放课程视频的状态");
}
@Override
public void speed() {
System.out.println("快进了视频");
super.videoStateContext.setVideoVideoState(VideoStateContext.SPEED_STATE);
}
@Override
public void pause() {
System.out.println("暂停了视频");
super.videoStateContext.setVideoVideoState(VideoStateContext.PAUSE_STATE);
}
@Override
public void stop() {
System.out.println("停止了视频");
super.videoStateContext.setVideoVideoState(VideoStateContext.STOP_STATE);
}
}
暂停状态类:
public class PauseState extends VideoVideoState {
@Override
public void paly() {
System.out.println("播放了视频");
super.videoStateContext.setVideoVideoState(VideoStateContext.PLAY_STATE);
}
@Override
public void speed() {
System.out.println("快进了视频");
super.videoStateContext.setVideoVideoState(VideoStateContext.SPEED_STATE);
}
@Override
public void pause() {
System.out.println("暂停播放课程视频的状态");
}
@Override
public void stop() {
System.out.println("停止了视频");
super.videoStateContext.setVideoVideoState(VideoStateContext.STOP_STATE);
}
}
快进状态类:
public class SpeedState extends VideoVideoState {
@Override
public void paly() {
System.out.println("播放了视频");
super.videoStateContext.setVideoVideoState(VideoStateContext.PLAY_STATE);
}
@Override
public void speed() {
System.out.println("快进播放课程视频的状态");
}
@Override
public void pause() {
System.out.println("暂停了视频");
super.videoStateContext.setVideoVideoState(VideoStateContext.PAUSE_STATE);
}
@Override
public void stop() {
System.out.println("停止了视频");
super.videoStateContext.setVideoVideoState(VideoStateContext.STOP_STATE);
}
}
停止状态类:
public class StopState extends VideoVideoState {
@Override
public void paly() {
System.out.println("播放了视频");
super.videoStateContext.setVideoVideoState(VideoStateContext.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 VideoStateContext {
// 与视频状态类 互相嵌套
private VideoVideoState videoVideoState;
public final static PlayState PLAY_STATE = new PlayState();
public final static SpeedState SPEED_STATE = new SpeedState();
public final static PauseState PAUSE_STATE = new PauseState();
public final static StopState STOP_STATE = new StopState();
public VideoStateContext() {
// 设置默认停止状态
this.videoVideoState = STOP_STATE;
this.videoVideoState.setVideoStateContext(this);
}
public VideoVideoState getVideoVideoState() {
return videoVideoState;
}
public void setVideoVideoState(VideoVideoState videoVideoState) {
// 切换状态
this.videoVideoState = videoVideoState;
this.videoVideoState.setVideoStateContext(this);
}
public void paly() {
this.videoVideoState.paly();
}
public void speed() {
this.videoVideoState.speed();
}
public void pause() {
this.videoVideoState.pause();
}
public void stop() {
this.videoVideoState.stop();
}
}
测试类:
public class Test {
public static void main(String[] args) {
VideoStateContext videoStateContext = new VideoStateContext();
videoStateContext.paly();
System.out.println("当前状态:" + videoStateContext.getVideoVideoState().getClass().getSimpleName());
videoStateContext.pause();
System.out.println("当前状态:" + videoStateContext.getVideoVideoState().getClass().getSimpleName());
videoStateContext.speed();
System.out.println("当前状态:" + videoStateContext.getVideoVideoState().getClass().getSimpleName());
videoStateContext.stop();
System.out.println("当前状态:" + videoStateContext.getVideoVideoState().getClass().getSimpleName());
videoStateContext.speed();
}
}
输出:
播放了视频
当前状态:PlayState
暂停了视频
当前状态:PauseState
快进了视频
当前状态:SpeedState
停止了视频
当前状态:StopState
ERROR 停止状态不能快进!
类图:
三、源码示例
1、jsf中的LifeCycle
赞 (0)