1.定义:允许一个对象在其内部状态改变时,改变它的行为。
2.类型:行为型
3.适用场景:一个对象存在多个状态(不同状态下行为不同),且状态可相互转换。
4.优点:将不同的状态隔离,把各种状态的转换逻辑,分布到State的子类中,减少相互间依赖;
增加新的状态非常简单。5.缺点:状态多的业务场景导致类数目增加,系统变得复杂
6.相关设计模式:享元模式
7.实例目录package
8.实例UML类图
9.代码
1 package com.geely.design.pattern.behavioral.state; 2 3 public abstract class CourseVideoState { 4 protected CourseVideoContext courseVideoContext; 5 6 public void setCourseVideoContext(CourseVideoContext courseVideoContext) { 7 this.courseVideoContext = courseVideoContext; 8 } 9 10 public abstract void play();11 public abstract void speed();12 public abstract void pause();13 public abstract void stop();14 }
1 package com.geely.design.pattern.behavioral.state; 2 3 public class PlayState extends CourseVideoState { 4 @Override 5 public void play() { 6 System.out.println("正常播放课程视频状态"); 7 } 8 9 @Override10 public void speed() {11 super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);12 }13 14 @Override15 public void pause() {16 super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);17 }18 19 @Override20 public void stop() {21 super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);22 }23 }
1 package com.geely.design.pattern.behavioral.state; 2 3 public class SpeedState extends CourseVideoState { 4 @Override 5 public void play() { 6 super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE); 7 } 8 9 @Override10 public void speed() {11 System.out.println("快进播放课程视频状态");12 }13 14 @Override15 public void pause() {16 super.courseVideoContext.setCourseVideoState(CourseVideoContext.PAUSE_STATE);17 }18 19 @Override20 public void stop() {21 super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);22 }23 }
1 package com.geely.design.pattern.behavioral.state; 2 3 public class PauseState extends CourseVideoState { 4 @Override 5 public void play() { 6 super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE); 7 } 8 9 @Override10 public void speed() {11 super.courseVideoContext.setCourseVideoState(CourseVideoContext.SPEED_STATE);12 }13 14 @Override15 public void pause() {16 System.out.println("暂停播放课程视频状态");17 }18 19 @Override20 public void stop() {21 super.courseVideoContext.setCourseVideoState(CourseVideoContext.STOP_STATE);22 }23 }
1 package com.geely.design.pattern.behavioral.state; 2 3 public class StopState extends CourseVideoState { 4 @Override 5 public void play() { 6 super.courseVideoContext.setCourseVideoState(CourseVideoContext.PLAY_STATE); 7 } 8 9 @Override10 public void speed() {11 System.out.println("ERROR 停止状态不能快进!!");12 }13 14 @Override15 public void pause() {16 System.out.println("ERROR 停止状态不能暂停!!");17 }18 19 @Override20 public void stop() {21 System.out.println("停止播放课程视频状态");22 }23 }
1 package com.geely.design.pattern.behavioral.state; 2 3 public class CourseVideoContext { 4 private CourseVideoState courseVideoState; 5 6 public final static PlayState PLAY_STATE = new PlayState(); 7 public final static SpeedState SPEED_STATE = new SpeedState(); 8 public final static PauseState PAUSE_STATE = new PauseState(); 9 public final static StopState STOP_STATE = new StopState();10 11 public CourseVideoState getCourseVideoState() {12 return courseVideoState;13 }14 15 public void setCourseVideoState(CourseVideoState courseVideoState) {16 this.courseVideoState = courseVideoState;17 this.courseVideoState.setCourseVideoContext(this);18 }19 20 public void play(){21 this.courseVideoState.play();22 }23 public void speed(){24 this.courseVideoState.speed();25 }26 public void stop(){27 this.courseVideoState.stop();28 }29 public void pause(){30 this.courseVideoState.pause();31 }32 }
1 package com.geely.design.pattern.behavioral.state; 2 3 public class Test { 4 public static void main(String[] args) { 5 CourseVideoContext courseVideoContext = new CourseVideoContext(); 6 courseVideoContext.setCourseVideoState(new PlayState()); 7 8 System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName()); 9 10 courseVideoContext.pause();11 System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName());12 13 courseVideoContext.speed();14 System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName());15 16 courseVideoContext.stop();17 System.out.println("当前状态:" + courseVideoContext.getCourseVideoState().getClass().getSimpleName());18 19 courseVideoContext.speed();20 }21 }