定义:当一个对象的内在状态改变时,允许改变其行为,这个对象看起来像是改变了其类
首先从这个名字上看,状态,比如视频播放,它有一些状态,比如 暂停 播放 停止,快速,这些状态是可以切换的,
在普通的人们心中可能会定义几个static变量或者枚举的方式来对状态的切换,但是如果新增的需求 可能要修改源代码,这就违背了设计模式的开闭原则,就是对扩展开放 对修改关闭,这个时候就适合用状态模式了,用代码说话,
VideoState.java视频的几个状态封装类
package com.kd.principle.state;
public abstract class VideoState {
public VideoStateContext videoStateContext;
public void setVideoStateContext(VideoStateContext videoStateContext) {
this.videoStateContext = videoStateContext;
}
public abstract void play();
public abstract void stop();
public abstract void speed();
public abstract void pause();
}
下面是四个状态类:
public class PauseState extends VideoState {
@Override
public void play() {
super.videoStateContext.setVideoState(VideoStateContext.PLAY_STATE);
}
@Override
public void stop() {
super.videoStateContext.setVideoState(VideoStateContext.STOP_STATE);
}
@Override
public void speed() {
super.videoStateContext.setVideoState(VideoStateContext.SPEED_STATE);
}
@Override
public void pause() {
System.out.println("暂停视频播放");
}
}
public class PlayState extends VideoState {
@Override
public void play() {
System.out.println("正在播放NBA总决赛");
}
@Override
public void stop() {
System.out.println("切换到停止状态");
super.videoStateContext.setVideoState(VideoStateContext.STOP_STATE);
}
@Override
public void speed() {
super.videoStateContext.setVideoState(VideoStateContext.SPEED_STATE);
}
@Override
public void pause() {
super.videoStateContext.setVideoState(VideoStateContext.PAUSE_STATE);
}
}
public class SpeedState extends VideoState {
@Override
public void play() {
super.videoStateContext.setVideoState(VideoStateContext.STOP_STATE);
}
@Override
public void stop() {
super.videoStateContext.setVideoState(VideoStateContext.STOP_STATE);
}
@Override
public void speed() {
System.out.println("快进播放视频");
}
@Override
public void pause() {
super.videoStateContext.setVideoState(VideoStateContext.PAUSE_STATE);
}
}
public class StopState extends VideoState {
@Override
public void play() {
super.videoStateContext.setVideoState(VideoStateContext.PLAY_STATE);
}
@Override
public void stop() {
System.out.println("停止视频播放");
}
@Override
public void speed() {
super.videoStateContext.setVideoState(VideoStateContext.SPEED_STATE);
}
@Override
public void pause() {
super.videoStateContext.setVideoState(VideoStateContext.PAUSE_STATE);
}
}
VideoStateContext.java是状态切换的上下文
public class VideoStateContext {
private VideoState videoState;
public static final PauseState PAUSE_STATE = new PauseState();
public static final StopState STOP_STATE = new StopState();
public static final SpeedState SPEED_STATE = new SpeedState();
public static final PlayState PLAY_STATE = new PlayState();
public VideoState getVideoState() {
return videoState;
}
public void setVideoState(VideoState videoState) {
this.videoState = videoState;
this.videoState.setVideoStateContext(this);
}
public void play() {
this.videoState.play();
}
public void stop() {
this.videoState.stop();
}
public void speed() {
this.videoState.speed();
}
public void pause() {
this.videoState.pause();
}
}
上层调用状态切换是通过上下来切换的,现在再通过它的定义来看看这例子:
当一个对象的内在状态改变时,允许改变其行为,这个对象看起来像是改变了其类
这里面有几个关键词 一个对象 状态改变 行为。 改变的类
一个对象就是VideoStateContext 它之所以能切换状态是因为它继承了VideoState ,VideoStateContext是对外调用的或者使用的
改变状态:比如我之前是播放状态 我切换到暂停状态, 这是一个状态的改变,
行为呢? 因为是通过
videoStateContext.stop();
其实最终是调用了PlayState中的stop()方法来进行状态的切换 定义后面几个字像是改变了其类就是指PlayState类,
如果有新的状态出现 那么只要继承VideoState,然后再VideoStateCotext中创建一个状态的对象即可,维护起来很方便
缺点是状态很多时,会增加很多类.