23种设计模式之命令模式

news/2024/7/17 8:30:59

定义:将一个请求封装为一个对象,从而使我们可用不同的请求对客户进行参数化;对请求排队或者记录请求日志,以及支持可以撤销的操作,也称之为:动作Action模式,事务transaction模式。
结构:

  • Command抽象命令类

  • ConcreterCommand具体命令类

  • Invoker调用者/请求者

  • Receiver接收者

  • Client客户类

命令接口:定义一个执行方法

/**
 * 命令
 */
public interface Command {
    void execute();
}

真正执行者

/**
 * 真正的执行者
 */
public class Receiver {
    public void action(){
        System.out.println("开火");
    }
}

具体命令

public class ConcreteCommand implements Command {
    Receiver receiver;

    public ConcreteCommand(Receiver receiver) {
        this.receiver = receiver;
    }

    @Override
    public void execute() {
        //命令真正执行前或后,执行相关的处理!
        receiver.action();
    }
}

发起者

/**
 * 发起者
 */
public class Invoke {
    private Command command;

    public Invoke(Command command) {
        this.command = command;
    }

    public void call() {
        command.execute();
    }
}

http://www.niftyadmin.cn/n/3648930.html

相关文章

Genymotion常见问题整合与解决方案

为什么说是常见问题整合呢,因为我就是Genymotion最悲剧的使用者,该见过的问题,我基本都见过了,在此总结出这血的教训,望大家不要重蹈覆辙。常见问题1:Genymotion在开启模拟器时卡在了starting virtual devi…

day.js使用_使用Day.js解析,验证,处理和显示JavaScript中的日期和时间

day.js使用With it’s last release nearly a year ago, the most recent commit over 6 months ago, and hundreds of open bugs and pull requests, it’s starting to seem like Moment.js is slowing down and it’s time to shop for more actively maintained alternativ…

23种设计模式之解释器模式

介绍 是一种不常用的设计模式用于描述如何构成一个简单的语言解释器,主要用于使用面向对象语言开发的编译器和解释器设计。当我们需要开发一种新的语言时,可以考虑使用解释器模式。 开发中常见的场景 EL表达式式的处理正则表达式解释器。SQL语法的计数…

字符串indexof方法_探索JavaScript中的字符串和数组的indexOf方法

字符串indexof方法When you need to find an element in an array or a string, indexOf() is one of your best friends. 当您需要在数组或字符串中查找元素时, indexOf()是您最好的朋友之一。 数组中的indexOf (indexOf in Arrays) Code first, explanation la…

Android(Lollipop/5.0) Material Design(八) 保持兼容性

Define Alternative Styles 定义替代样式 让你的app,使用Material Design的主题运行在支持它的设备上,并在早期版本的设备上可以运行较早的主题:1. 在res/values/styles.xml 定义一个主题继承较早的主题2. 在res/values-v21/styles.xml 定义…

软件测试管理--第一章 1.1节

第一部分 基础篇第1章 测试管理概论“我们这个项目的测试人员太少了!”“能否给我们提供一台新的测试服务器!”“需要延长一个星期的进度才可以完成测试工作!”“开发人员压根就没有修改缺陷!我们还测试什么!”“怎…

如何在CentOS 7上安装和使用PostgreSQL

介绍 (Introduction) Relational database management systems are a key component of many web sites and applications. They provide a structured way to store, organize, and access information. 关系数据库管理系统是许多网站和应用程序的关键组成部分。 它们提供了一…

23种设计模式之模板方法模式(template method)

场景:我们去银行,一般分三步,取号排队,办理具体的业务 ,反馈评分。只有办具体的业务我们是不确定的,今天我有可能取钱,明天有可能存钱,也有可能后天去和妹子搭讪。这时候我们就可以做…