当前位置:网站首页>命令模式(Command)
命令模式(Command)
2022-08-04 10:52:00 【pmc0_0】
问题
我们要把将军(请求方)和士兵(执行方)分离开
- Receive接收者角色
该角色就是干活的角色,命令传递到这里是应该被执行的,具体到我们上面的例子中就是弓箭手和冲锋士兵的两个实现类。
- Command命令角色
需要执行的所有命令都在这里声明。
- Invoker调用者角色
接收到命令,并执行命令。将军就是这个角色。
命令模式比较简单,但是在项目中非常频繁地使用,因为它的封装性非常好,把请求方(Invoker)和执行方(Receiver)分开了,扩展性也有很好的保障,通用代码比较简单。先看一下Receiver类
模式引入
其实就是通过负责人发布命令,命令关联着具体的执行者,让执行者去执行自己的行为
package command;
/** * @author pangjian * @ClassName Receiver * @Description 命令实施类,相当于将军发布命令后,接收者实施命令 * @date 2022/7/29 22:00 */
public abstract class Receiver {
public abstract void doSomething();
}
package command;
/** * @author pangjian * @ClassName Soldiers * @Description 士兵,自己的业务是冲锋 * @date 2022/7/29 22:19 */
public class Soldiers extends Receiver{
@Override
public void doSomething() {
System.out.println("士兵冲锋");
}
}
package command;
/** * @author pangjian * @ClassName Archer * @Description 弓箭手,业务是发射弓箭 * @date 2022/7/29 22:17 */
public class Archer extends Receiver {
@Override
public void doSomething() {
System.out.println("弓箭手发射弓箭");
}
}
- 命令
package command;
/** * @author pangjian * @Interface Commod * @Description 命令接口 * @date 2022/7/29 21:58 */
public interface Command {
void execute();
}
package command;
/** * @author pangjian * @ClassName ConcreteCommand * @Description 具体命令1:弓箭手发射弓箭 * @date 2022/7/29 22:01 */
public class ConcreteCommand1 implements Command{
private Receiver receiver;
public ConcreteCommand1(Receiver receiver) {
this.receiver = receiver;
}
/** * @Description:命令类去驱动接收者实施命令 * @return void * @date 2022/7/29 22:31 */
@Override
public void execute() {
this.receiver.doSomething();
}
}
package command;
/** * @author pangjian * @ClassName ConcreteCommand2 * @Description 具体命令2: 士兵冲锋 * @date 2022/7/29 22:13 */
public class ConcreteCommand2 implements Command{
private Receiver receiver;
public ConcreteCommand2(Receiver receiver) {
this.receiver = receiver;
}
@Override
public void execute() {
this.receiver.doSomething();
}
}
- 负责人
package command;
/** * @author pangjian * @ClassName Invoker * @Description 命令调用者(相当于将军) * @date 2022/7/29 21:59 */
public class Invoker {
private Command command;
/* * @Description: 相当于皇帝给你一个命令,将军然后命令发布 * @Param command: 命令 * @return void * @date 2022/7/29 22:11 */
public void setCommand(Command command) {
this.command = command;
}
/* * @Description: 命令执行 * @return void * @date 2022/7/29 22:12 */
public void action() {
this.command.execute();
}
}
测试
package command;
/** * @author pangjian * @ClassName Client * @Description TODO * @date 2022/7/29 22:01 */
public class Client {
public static void main(String[] args) {
Invoker invoker = new Invoker();
Receiver receiver = new Archer();
Receiver receiver1 = new Soldiers();
Command command1 = new ConcreteCommand1(receiver);
Command command2 = new ConcreteCommand2(receiver1);
invoker.setCommand(command1);
invoker.action();
invoker.setCommand(command2);
invoker.action();
}
}
边栏推荐
猜你喜欢
随机推荐
AWS Lambda related concepts and implementation approach
八、MFC对话框
linux下数据库初始化密码
helm安装
MySQL:面试问的范式设计
【虹科案例】基于3D相机组装家具
Google Earth Engine APP ——制作上传GIF动图并添加全球矢量位置
ORA-00054 资源正忙
winform 在Datatable插入一笔数据
Business collocations
Use pytest hook function to realize automatic test result push enterprise WeChat
Heap Sort
Win11 file types, how to change?Win11 modify the file suffix
低代码是开发的未来吗?浅谈低代码开发平台的发展现状及未来趋势
MySQL: Integrity Constraints and Table Design Principles
DB2查看执行过长的SQL
学会使用set和map的基本接口
OD-Model【5】:YOLOv1
自己实现一个枚举validation校验器
3-5年以上的功能测试如何进阶自动化?