一、 状态模式介绍
二、代码演示
三、jdk中使用 状态模式的地方
一、 状态模式介绍(重要)
定义:允许一个对象在其内部状态改变时,改变它的行为
类型:行为型
适用场景:
一个对象存在多个状态(不同状态下行为不同),且状态可以相互转换
优点:
将不同的状态隔离
把各种状态的转换逻辑,分不到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();
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();
}
}
//课程状态抽象接口
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();
}
//正常播放状态类
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);
}
}
//暂停状态类
public class PauseState extends CourseVideoState{
@Override
public void play() {
super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
}
@Override
public void speed() {
System.out.println("暂停状态,无法进入加速状态");
}
@Override
public void pause() {
System.out.println("暂停状态");
}
@Override
public void stop() {
super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_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);
}
}
//停止状态类
public class StopState extends CourseVideoState{
@Override
public void play() {
super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE);
}
@Override
public void speed() {
System.out.println("停止状态,无法进入加速状态");
}
@Override
public void pause() {
System.out.println("停止状态,无法进入暂停状态");
}
@Override
public void stop() {
System.out.println("停止状态");
}
}
//测试类
public class Test {
public static void main(String[] args) {
CourseVideoContext courseVideoContext=new CourseVideoContext();
courseVideoContext.setCourseVideoState(new PlayState());
System.out.println("当前状态:"+courseVideoContext.getCourseVideoState().getClass().getName());
courseVideoContext.pause();
System.out.println("当前状态:"+courseVideoContext.getCourseVideoState().getClass().getName());
courseVideoContext.stop();
System.out.println("当前状态:"+courseVideoContext.getCourseVideoState().getClass().getName());
courseVideoContext.pause();
System.out.println("当前状态:"+courseVideoContext.getCourseVideoState().getClass().getName());
}
}
来看一下类的uml图:
运行结果:
可以从图中看出,状态控制中,是无法从停止状态进入暂停状态的。
三、jdk中使用 状态模式的地方
1、java.util.Iterator
2、 javax.faces.lifecycle.LifeCycle#execute()