状态模式
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
package top.fjy8018.designpattern.pattern.behavior.state;
|
||||
|
||||
/**
|
||||
* 视频上下文
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2020/7/22 15:20
|
||||
*/
|
||||
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 stop() {
|
||||
this.courseVideoState.stop();
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
this.courseVideoState.pause();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package top.fjy8018.designpattern.pattern.behavior.state;
|
||||
|
||||
import javax.faces.lifecycle.Lifecycle;
|
||||
|
||||
/**
|
||||
* 视频播放状态转换场景
|
||||
* <p>
|
||||
* {@link Lifecycle#execute(javax.faces.context.FacesContext)} 该方法通过外部状态控制改变行为
|
||||
* {@link javax.faces.webapp.FacesServlet} 状态控制
|
||||
*
|
||||
* @author F嘉阳
|
||||
* @date 2020/7/22 15:20
|
||||
*/
|
||||
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();
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package top.fjy8018.designpattern.pattern.behavior.state;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2020/7/22 15:20
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package top.fjy8018.designpattern.pattern.behavior.state;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2020/7/22 15:20
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package top.fjy8018.designpattern.pattern.behavior.state;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2020/7/22 15:20
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package top.fjy8018.designpattern.pattern.behavior.state;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2020/7/22 15:20
|
||||
*/
|
||||
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("停止播放课程视频状态");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package top.fjy8018.designpattern.pattern.behavior.state;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author F嘉阳
|
||||
* @date 2020/7/22 16:19
|
||||
*/
|
||||
class PlayStateTest {
|
||||
|
||||
@Test
|
||||
void play() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user