1.定义(http://en.wikipedia.org/wiki/Command_pattern#Java)

In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to represent and encapsulate all the information needed to call a method at a later time. This information includes the method name, the object that owns the method and values for the method parameters.

Four terms always associated with the command pattern are commandreceiverinvoker and client. A command object has a receiver object and invokes a method of the receiver in a way that is specific to that receiver's class. The receiver then does the work. A command object is separately passed to an invoker object, which invokes the command, and optionally does bookkeeping about the command execution. Any command object can be passed to the same invoker object. Both an invoker object and several command objects are held by a client object. The client contains the decision making about which commands to execute at which points. To execute a command, it passes the command object to the invoker object. See example code below.

Using command objects makes it easier to construct general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the class of the method or the method parameters. Using an invoker object allows bookkeeping about command executions to be conveniently performed, as well as implementing different modes for commands, which are managed by the invoker object, without the need for the client to be aware of the existence of bookkeeping or modes..

2. uml图(http://www.th7.cn/Program/java/2012/03/23/65776.shtml)

3. 实例(http://www.th7.cn/Program/java/2012/03/23/65776.shtml)

//抽象接收者,定义了每个接收者应该完成的业务逻辑
abstract class AbstractReceiver {  public abstract void doJob();
}  // 具体接收者01,实现自己真正的业务逻辑
class Receiver01 extends AbstractReceiver {  public void doJob() {  System.out.println("接收者01 完成工作 .../n");  }
}  // 具体接收者02,实现自己真正的业务逻辑
class Receiver02 extends AbstractReceiver {  public void doJob() {  System.out.println("接收者02 完成工作 .../n");  }
}
命令类:
// 抽象命令类,定义了每个具体命令被执行的入口方法execute()
abstract class AbstractCommand {  public abstract void execute();
}  // 具体命令类01,通过构造函数的参数决定了该命令由哪个接收者执行
class Command01 extends AbstsractCommand {  private AbstractReceiver receiver = null;  public Command01(AbstractReceiver receiver) {  this.receiver = receiver;  }  public void execute() {  System.out.println("命令01 被发布 ...");  this.receiver.doJob();  }
}  // 具体命令类02,通过构造函数的参数决定了该命令由哪个接收者执行
class Command02 extends AbstractCommand {  private AbstractReceiver receiver = null;  public Command02(AbstractReceiver receiver) {  this.receiver = receiver;  }  public void execute() {  System.out.println("命令02 被发布 ...");  this.receiver.doJob();  }
}
调用者类:
// 调用者,负责将具体的命令传送给具体的接收者
class Invoker {  private AbstractCommand command = null;  public void setCommand(AbstractCommand command) {  this.command = command;  }  public void action() {  this.command.execute();  }
} 

测试类

//测试类
public class Client {  public static void main(String[] args) {  // 创建调用者  Invoker invoker = new Invoker();  // 创建一个具体命令,并指定该命令被执行的具体接收者  AbstractCommand command01 = new Command01(new Receiver01());  // 给调用者发布一个具体命令
        invoker.setCommand(command01);  // 调用者执行命令,其实是将其传送给具体的接收者并让其真正执行
        invoker.action();  AbstractCommand command02 = new Command01(new Receiver02());  invoker.setCommand(command02);  invoker.action();  }
} 

测试结果

命令01 被发布 ...
接收者01 完成工作 ...命令02 被发布 ...
接收者02 完成工作 ...

转载于:https://www.cnblogs.com/davidwang456/p/3847986.html

command pattern相关推荐

  1. 设计模式系列3-----C++实现命令模式(Command Pattern)

    什么是命令模式? GoF的书的定义为:"Command pattern encapsulate request as an object, thereby letting you param ...

  2. 设计模式:命令模式(Command Pattern)

    命令模式(Command Pattern): 在软件设计中,我们经常需要向某些对象发送请求,但是并不知道请求的接受者是谁,也不知道请求的操作是哪个. 我们只需在程序运行时指定具体的请求接受者即可,此时 ...

  3. 设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释

    命令模式(command pattern) 撤销(undo) 详细解释 本文地址: http://blog.csdn.net/caroline_wendy 參考命令模式: http://blog.cs ...

  4. 设计模式之-命令模式(Command Pattern)

    命令模式(Command Pattern)是用来实现在一个请求 - 响应模型松耦合.在命令模式中,请求被发送给调用者和调用它传递给被封装的命令对象. Command对象将请求传递到接收器的适当的方法来 ...

  5. 乐在其中设计模式(C#) - 命令模式(Command Pattern)

    原文:乐在其中设计模式(C#) - 命令模式(Command Pattern) [索引页] [源码下载] 乐在其中设计模式(C#) - 命令模式(Command Pattern) 作者:webabcd ...

  6. 行为型设计模式(3)—— 命令模式(Command Pattern)

    文章目录 1.概述 2.命令模式简单实现 3.命令模式的应用场景和优缺点 4.小结 参考文献 1.概述 使用设计模式可以提高代码的可复用性.可扩充性和可维护性.命令模式(Command Pattern ...

  7. Command Pattern的简单介绍

    Command pattern 的角色有:Command(抽象命令).ConcreteCommand(具体命令) .Invoker(传达命令者) .receiver(接收命令者)Client(客户类, ...

  8. 秒懂设计模式之命令模式(Command Pattern)

    [版权申明] 非商业目的注明出处可自由转载 博文地址:https://blog.csdn.net/ShuSheng0007/article/details/116115743 出自:shusheng0 ...

  9. C++设计模式——命令模式(command pattern)

    一.原理讲解 别名动作(action),事务(transaction). 1.1意图 将一个请求封装为一个对象,从而使我们可用不同的请求对客户进行参数化,对请求排队或记录请求日志,以及支持可撤销的操作 ...

最新文章

  1. 什么是棉绒,它如何节省您的时间?
  2. 【QGIS入门实战精品教程】9.1:QGIS构建泰森多边形(Thiessen Polygon)实例精解
  3. linux 协议错误,在linux客户机上:协议错误,Vagrant无法挂载同步的文件夹_vagrant_开发99编程知识库...
  4. PHP 设计模式 笔记与总结(3)SPL 标准库
  5. 金蝶记账王登录显示连接金蝶云服务器异常,金蝶KIS记账王系统初始化常见问题...
  6. 现代操作系统原理与实践04:实验1:机器启动
  7. Fedora 安装 WPS
  8. 【sscom】 串口调试工具
  9. 平衡车gazebo仿真
  10. 通用权限管理概要设计说明书
  11. 3D变化——旋转的立方体
  12. 商旅信用卡(多重继承)
  13. CentOS 识别NTFS格式U盘
  14. 解决RuntimeException: Parcel android.os.Parcel@*: Unmarshalling unknown type code * at offset * 异常
  15. python画蜡烛致敬烈士_Matplotlib 蜡烛图教程
  16. QT PRO工程文件区分Debug和Release方法
  17. 基于区块链的电子档案及其流转管理系统
  18. CGED2020总结
  19. 高级软件工程师证书有用吗_bim工程师证书有用吗?bim好在哪里?
  20. 42、【斯纳克图书馆管理系统】编目流程 [ 打印编号]

热门文章

  1. python定义字典列表_[Python基础]五、列表、元组和字典
  2. 小白重装系统教程_重装解决99%的电脑问题:小白U盘重装系统教程
  3. c# bitmap 去除噪点_黑头怎么去除最有效用盐处理的方法推荐
  4. java int == integer_java int与integer的区别
  5. mysql 浏览器可视窗口_浏览器窗口的可视区域大小指的是哪里?
  6. 红帽linux lnmp搭建,Linux(redhat5.4)下lnmp环境的搭建
  7. java nginx tomcat_Nginx + Tomcat (java )服务器部署
  8. linux raid更换硬盘,linux系统raid1更换故障硬盘处理过程
  9. c语言输入r1 r2垫片的面积,2011学生C语言上机实验
  10. php改密后joomla无法登陆,恢复丢失的Joomla密码的最常用方法