状态模式

This commit is contained in:
2020-07-22 16:29:21 +08:00
parent 52b24d0111
commit dd1da27b7c
13 changed files with 256 additions and 0 deletions

2
.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,2 @@
# Default ignored files
/workspace.xml

2
.idea/Design-pattern.iml generated Normal file
View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4" />

1
.idea/encodings.xml generated
View File

@@ -2,5 +2,6 @@
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
</component>
</project>

20
.idea/jarRepositories.xml generated Normal file
View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
</component>
</project>

3
.idea/misc.xml generated
View File

@@ -1,6 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>

View File

@@ -134,6 +134,12 @@
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.sun.faces/jsf-api -->
<dependency>
<groupId>com.sun.faces</groupId>
<artifactId>jsf-api</artifactId>
<version>2.2.20</version>
</dependency>
<dependency>
<groupId>mysql</groupId>

View File

@@ -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();
}
}

View File

@@ -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();
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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("停止播放课程视频状态");
}
}

View File

@@ -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();
}
}