最通俗易懂的命令模式讲解(命令模式百科)
wptr33 2025-06-13 17:20 5 浏览
我们先不讲什么是命令模式,先通过一个场景来引出命令模式,看看命令模式能解决什么样的问题。
现在有一个渣男张三,他有还几个女朋友,你现在是不是还是单身狗,你就说你气不气?然后他需要每天分别叫几个女朋友起床和睡觉(就只是睡觉,别想歪)。那么我们怎么用代码来模拟实现这个事情呢?常规解决办法是我们建立一个女朋友的接口(毕竟女朋友多,得立规矩),然后渣男张三分别聚合多个女朋友,分别调用每个女朋友的起床和睡觉。类图大致如下:
类图不规范,凑合看吧,大致能理解想表达的意思就行了,不要在意这些细节。
这么设计有很多问题,首先就是张三和各个女朋友都是聚合关系,太亲密,不利于扩展,比如张三又新交一个女朋友,就得改张三的代码,不符合开闭原则。说白了就是不方便张三扩张业务。那么显然张三也不会同意你这么一通操作断了他的后路的。
那么此时就需要命令模式来一显身手了,命令模式可以将请求发送者和接收者完全解耦,发送者与接收者之间没有直接引用关系,发送请求的对象只需要知道如何发送请求,而不必知道如何完成请求。
有点抽象,不过没关系,看不懂就不看。我们直接来看看类图。
这里的Command(命令)就是张三可以执行的操作,这里是一个接口或者抽象类。
Receiver(命令接收者)就是张三的女朋友(复数加s)。
Invoker(调用者)将两者整合到一起,负责调用命令对象执行请求。
GetUpCommand和SleepCommand是具体的命令操作。
根据类图写出来的代码如下:
public interface Command {
void execute();
void undo();
}
/** * 用作初始化 *
/public class NullCommand implements Command {
public void execute() {
}
public void undo() {
}
}
/**
* 命令接收者接口
**/
public interface Receiver {
public void getUp();
public void sleep();
}
/** * 女朋友1号小红 */
public class XiaoHong implements Receiver {
public void getUp(){
System.out.println("小红起来了");
}
public void sleep(){
System.out.println("小红睡下了");
}
}
public class XiaoHongGetUpCommand implements Command {
public XiaoHong xiaoHong;
public XiaoHongGetUpCommand(XiaoHong xiaoHong){
this.xiaoHong = xiaoHong;
}
public void execute() {
this.xiaoHong.getUp(); //小红起床
}
public void undo() {
this.xiaoHong.sleep(); //撤销,小红睡觉
}
}
public class XiaoHongSleepCommand implements Command {
public XiaoHong xiaoHong;
public XiaoHongSleepCommand(XiaoHong xiaoHong){
this.xiaoHong = xiaoHong;
}
public void execute() {
this.xiaoHong.sleep(); //小红睡觉
}
public void undo() {
this.xiaoHong.getUp(); //撤销,小红起床
}
}
public class Invoker {
private Command[] getUpCommand = new Command[5]; // 此处给渣男张三预留5个女朋友的位置 private Command[] sleepCommand = new Command[5]; // 同上 public Command undoCommand; //记录上一次操作,用作撤回命令 public Invoker(){
for (int i =0;i < 5;i++){
getUpCommand[i] = new NullCommand();
sleepCommand[i] = new NullCommand();
}
}
public void setCommand(int index, Command getUpCommand, Command sleepCommand){
this.getUpCommand[index] = getUpCommand;
this.sleepCommand[index] = sleepCommand;
}
public void setDown(int index){
undoCommand = getUpCommand[index];
getUpCommand[index].execute();
}
public void setUp(int index){
undoCommand = sleepCommand[index];
sleepCommand[index].execute();
}
/** * 撤销操作,起床的重新睡觉,睡觉的就起床 */ public void undo() {
undoCommand.undo();
}
}
public class Demo {
public static void main(String[] args) {
XiaoHong xiaoHong = new XiaoHong();
XiaoHongSleepCommand xiaoHongSleepCommand = new XiaoHongSleepCommand(xiaoHong);
XiaoHongGetUpCommand xiaoHongGetUpCommand = new XiaoHongGetUpCommand(xiaoHong);
invoker.setCommand(1, xiaoHongSleepCommand, xiaoHongGetUpCommand);
invoker.setUp(1);
invoker.setDown(1);
invoker.undo();
}
}
用命令模式就比较完美的解决张三的问题,而且还方便了张三继续找女朋友,他一定会感谢你的。如果新交了女朋友。比如来第二个女朋友小花,扩展方法如下:
/** * 女朋友2号小花 */
public class XiaoHua implements Receiver{
public void getUp(){
System.out.println("小花起来了");
}
public void sleep(){
System.out.println("小花躺下了");
}
}
public class XiaoHuaGetUpCommand implements Command {
public XiaoHua xiaoHua;
public XiaoHuaGetUpCommand(XiaoHua xiaoHua){
this.xiaoHua = xiaoHua;
}
public void execute() {
this.xiaoHua.getUp();
}
public void undo() {
this.xiaoHua.sleep();
}
}
public class XiaoHuaSleepCommand implements Command {
public XiaoHua xiaoHua;
public XiaoHuaSleepCommand(XiaoHua xiaoHua){
this.xiaoHua = xiaoHua;
}
public void execute() {
this.xiaoHua.sleep();
}
public void undo() {
this.xiaoHua.getUp();
}
}
public class Demo {
public static void main(String[] args) {
XiaoHong xiaoHong = new XiaoHong();
XiaoHongSleepCommand xiaoHongSleepCommand = new XiaoHongSleepCommand(xiaoHong);
XiaoHongGetUpCommand xiaoHongGetUpCommand = new XiaoHongGetUpCommand(xiaoHong);
invoker.setCommand(1, xiaoHongSleepCommand, xiaoHongGetUpCommand);
invoker.setUp(1);
invoker.setDown(1);
invoker.undo();
System.out.println("-----------------------------------");
XiaoHua xiaoHua = new XiaoHua();
XiaoHuaSleepCommand xiaoHuaSleepCommand = new XiaoHuaSleepCommand(xiaoHua);
XiaoHuaGetUpCommand xiaoHuaGetUpCommand = new XiaoHuaGetUpCommand(xiaoHua);
Invoker invoker = new Invoker();
invoker.setCommand(0, xiaoHuaSleepCommand, xiaoHuaGetUpCommand);
invoker.setUp(0);
invoker.setDown(0);
invoker.undo();
}
}
怎么样,是不是很方便!再回上面看看命令模式的定义,是不是有点明白?
相关推荐
- Linux文件系统操作常用命令(linux文件内容操作命令)
-
在Linux系统中,有一些常用的文件系统操作命令,以下是这些命令的介绍和作用:#切换目录,其中./代表当前目录,../代表上一级目录cd#查看当前目录里的文件和文件夹ls#...
- 别小看tail 命令,它难倒了技术总监
-
我把自己以往的文章汇总成为了Github,欢迎各位大佬star...
- lnav:基于 Linux 的高级控制台日志文件查看器
-
lnav是一款开源的控制台日志文件查看器,专为Linux和Unix-like系统设计。它通过自动检测日志文件的格式,提取时间戳、日志级别等关键信息,并将多个日志文件的内容按时间顺序合并显示,...
- 声明式与命令式代码(声明模式和命令模式)
-
编程范式中的术语和差异信不信由你,你可能已经以开发人员的身份使用了多种编程范例。因为没有什么比用编程理论招待朋友更有趣的了,所以这篇文章可以帮助您认识代码中的流行范例。命令式编程命令式编程是我们从As...
- linux中的常用命令(linux常用命令和作用)
-
linux中的常用命令linux中的命令统称shell命令shell是一个命令行解释器,将用户命令解析为操作系统所能理解的指令,实现用户与操作系统的交互shell终端:我们平时输入命令,执行程序的那个...
- 提高工作效率的--Linux常用命令,能够决解95%以上的问题
-
点击上方关注,第一时间接受干货转发,点赞,收藏,不如一次关注评论区第一条注意查看回复:Linux命令获取linux常用命令大全pdf+Linux命令行大全pdf...
- 如何限制他人操作自己的电脑?(如何控制别人的电脑不让发现)
-
这段时间,小猪罗志祥正处于风口浪尖,具体是为啥?还不知道的小伙伴赶紧去补一下最近的娱乐圈八卦~简单来说,就是我们的小罗同事,以自己超强的体力,以及超强的时间管理能力,重新定义了「多人运动」的含义,重新...
- 最通俗易懂的命令模式讲解(命令模式百科)
-
我们先不讲什么是命令模式,先通过一个场景来引出命令模式,看看命令模式能解决什么样的问题。现在有一个渣男张三,他有还几个女朋友,你现在是不是还是单身狗,你就说你气不气?然后他需要每天分别叫几个女朋友起床...
- 互联网大厂后端必看!Spring Boot 中Runtime执行与停止命令?
-
你是否曾在使用SpringBoot开发项目时,遇到需要执行系统命令的场景?比如调用脚本进行文件处理,又或是启动外部程序?很多后端开发人员会使用Processexec=Runtime.get...
- Linux 常用命令(linux常用的20个命令面试)
-
日志排查类操作命令...
- Java字节码指令:if_icmpgt(0xA3)(java字节码使用的汇编语言)
-
if_icmpgt是Java字节码中的一条条件跳转指令,其全称是"IfIntegerCompareGreaterThan"。它用于比较两个整数值的大小。如果栈顶的第一个...
- 外贸干货|如何增加领英的曝光量和询盘
-
#跨境电商#...
- golang执行linux命令(golang调用shell脚本)
-
需求需要通过openssl生成rsa秘钥,然后保存该秘钥。代码实例packagemainimport("io/ioutil""bytes"&...
- LINUX磁盘挂载(linux磁盘挂载到windows)
-
1、使用root用户查看磁盘挂载情况:fdisk-l2、使用df查看当前磁盘挂载情况,根据和fdisk-l的结果进行对比,查看还有那些磁盘未使用3、挂载:mount磁盘挂载路径...
- Linux命令学习——nl命令(linux ln命令的使用)
-
nl命令主要功能为每一个文件添加行号,每一个输入的文件添加行号后发送到标准输出。当没有文件或文件为-时,读取标准输入...
- 一周热门
-
-
C# 13 和 .NET 9 全知道 :13 使用 ASP.NET Core 构建网站 (1)
-
因果推断Matching方式实现代码 因果推断模型
-
git pull命令使用实例 git pull--rebase
-
面试官:git pull是哪两个指令的组合?
-
git 执行pull错误如何撤销 git pull fail
-
git fetch 和git pull 的异同 git中fetch和pull的区别
-
git pull 和git fetch 命令分别有什么作用?二者有什么区别?
-
git pull 之后本地代码被覆盖 解决方案
-
还可以这样玩?Git基本原理及各种骚操作,涨知识了
-
git命令之pull git.pull
-
- 最近发表
- 标签列表
-
- git pull (33)
- git fetch (35)
- mysql insert (35)
- mysql distinct (37)
- concat_ws (36)
- java continue (36)
- jenkins官网 (37)
- mysql 子查询 (37)
- python元组 (33)
- mybatis 分页 (35)
- vba split (37)
- redis watch (34)
- python list sort (37)
- nvarchar2 (34)
- mysql not null (36)
- hmset (35)
- python telnet (35)
- python readlines() 方法 (36)
- munmap (35)
- docker network create (35)
- redis 集合 (37)
- python sftp (37)
- setpriority (34)
- c语言 switch (34)
- git commit (34)