命令模式
This commit is contained in:
@@ -0,0 +1,20 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.command;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 关闭视频命令
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/7/21 15:58
|
||||||
|
*/
|
||||||
|
public class CloseCourseVideoCommand implements Command {
|
||||||
|
private final CourseVideo courseVideo;
|
||||||
|
|
||||||
|
public CloseCourseVideoCommand(CourseVideo courseVideo) {
|
||||||
|
this.courseVideo = courseVideo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
courseVideo.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.command;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 命令抽象
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/7/21 15:58
|
||||||
|
*/
|
||||||
|
public interface Command {
|
||||||
|
void execute();
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.command;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 操作视频
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/7/21 15:58
|
||||||
|
*/
|
||||||
|
public class CourseVideo {
|
||||||
|
private final String name;
|
||||||
|
|
||||||
|
public CourseVideo(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void open() {
|
||||||
|
System.out.println(this.name + "课程视频开放");
|
||||||
|
}
|
||||||
|
|
||||||
|
public void close() {
|
||||||
|
System.out.println(this.name + "课程视频关闭");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.command;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开视频命令
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/7/21 15:58
|
||||||
|
*/
|
||||||
|
public class OpenCourseVideoCommand implements Command {
|
||||||
|
private final CourseVideo courseVideo;
|
||||||
|
|
||||||
|
public OpenCourseVideoCommand(CourseVideo courseVideo) {
|
||||||
|
this.courseVideo = courseVideo;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void execute() {
|
||||||
|
courseVideo.open();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.command;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 命令执行人
|
||||||
|
*
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/7/21 15:58
|
||||||
|
*/
|
||||||
|
public class Staff {
|
||||||
|
/**
|
||||||
|
* 保证执行顺序
|
||||||
|
*/
|
||||||
|
private final List<Command> commandList = new ArrayList<Command>();
|
||||||
|
|
||||||
|
public void addCommand(Command command) {
|
||||||
|
commandList.add(command);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void executeCommands() {
|
||||||
|
for (Command command : commandList) {
|
||||||
|
command.execute();
|
||||||
|
}
|
||||||
|
commandList.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
package top.fjy8018.designpattern.pattern.behavior.command;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author F嘉阳
|
||||||
|
* @date 2020/7/20 16:02
|
||||||
|
*/
|
||||||
|
class CommandTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void doTest() {
|
||||||
|
CourseVideo courseVideo = new CourseVideo("Java设计模式精讲");
|
||||||
|
OpenCourseVideoCommand openCourseVideoCommand = new OpenCourseVideoCommand(courseVideo);
|
||||||
|
CloseCourseVideoCommand closeCourseVideoCommand = new CloseCourseVideoCommand(courseVideo);
|
||||||
|
|
||||||
|
Staff staff = new Staff();
|
||||||
|
|
||||||
|
staff.addCommand(openCourseVideoCommand);
|
||||||
|
staff.addCommand(closeCourseVideoCommand);
|
||||||
|
|
||||||
|
staff.executeCommands();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user