设计模式 指令模式

Command Pattern is one of the Behavioral Design Pattern. Command design pattern is used to implement loose coupling in a request-response model.

命令模式是行为设计​​模式之一。 命令设计模式用于在请求-响应模型中实现松耦合

命令模式 (Command Pattern)

In command pattern, the request is send to the invoker and invoker pass it to the encapsulated command object.

在命令模式中,请求被发送到invoker ,调用者将其传递给封装的command对象。

Command object passes the request to the appropriate method of Receiver to perform the specific action.

命令对象将请求传递给Receiver的适当方法以执行特定操作。

The client program create the receiver object and then attach it to the Command. Then it creates the invoker object and attach the command object to perform an action.

客户端程序创建接收器对象,然后将其附加到命令。 然后,它创建调用者对象并附加命令对象以执行操作。

Now when client program executes the action, it’s processed based on the command and receiver object.

现在,当客户端程序执行动作时,将根据命令和接收器对象对其进行处理。

命令设计模式示例 (Command Design Pattern Example)

We will look at a real life scenario where we can implement Command pattern. Let’s say we want to provide a File System utility with methods to open, write and close file. This file system utility should support multiple operating systems such as Windows and Unix.

我们将看一个可以实现Command模式的现实生活场景。 假设我们要为File System实用程序提供打开,写入和关闭文件的方法。 该文件系统实用程序应支持多种操作系统,例如Windows和Unix。

To implement our File System utility, first of all we need to create the receiver classes that will actually do all the work.

要实现我们的文件系统实用程序,首先,我们需要创建将实际完成所有工作的接收器类。

Since we code in terms of interface in java, we can have FileSystemReceiver interface and it’s implementation classes for different operating system flavors such as Windows, Unix, Solaris etc.

由于我们使用Java中的接口进行编码,因此我们可以拥有FileSystemReceiver接口,并且它是用于不同操作系统风格的实现类,例如Windows,Unix,Solaris等。

命令模式接收器类 (Command Pattern Receiver Classes)

package com.journaldev.design.command;public interface FileSystemReceiver {void openFile();void writeFile();void closeFile();
}

FileSystemReceiver interface defines the contract for the implementation classes. For simplicity, I am creating two flavors of receiver classes to work with Unix and Windows systems.

FileSystemReceiver接口定义实现类的协定。 为简单起见,我创建了两种类型的接收器类以与Unix和Windows系统一起使用。

package com.journaldev.design.command;public class UnixFileSystemReceiver implements FileSystemReceiver {@Overridepublic void openFile() {System.out.println("Opening file in unix OS");}@Overridepublic void writeFile() {System.out.println("Writing file in unix OS");}@Overridepublic void closeFile() {System.out.println("Closing file in unix OS");}}
package com.journaldev.design.command;public class WindowsFileSystemReceiver implements FileSystemReceiver {@Overridepublic void openFile() {System.out.println("Opening file in Windows OS");}@Overridepublic void writeFile() {System.out.println("Writing file in Windows OS");}@Overridepublic void closeFile() {System.out.println("Closing file in Windows OS");}}

Did you noticed the Override annotation and if you wonder why it’s used, please read java annotations and override annotation benefits.

您是否注意到Override注释,并且如果您想知道为什么使用它,请阅读Java注释并重写注释的好处 。

Now that our receiver classes are ready, we can move to implement our Command classes.

现在我们的接收器类已经准备好了,我们可以开始实现Command类了。

命令模式接口和实现 (Command Pattern Interface and Implementations)

We can use interface or abstract class to create our base Command, it’s a design decision and depends on your requirement.

我们可以使用接口或抽象类来创建基本Command,这是设计决定,取决于您的要求。

We are going with interface because we don’t have any default implementations.

我们要使用接口,因为我们没有任何默认实现。

package com.journaldev.design.command;public interface Command {void execute();
}

Now we need to create implementations for all the different types of action performed by the receiver. Since we have three actions we will create three Command implementations. Each Command implementation will forward the request to the appropriate method of receiver.

现在,我们需要为接收器执行的所有不同类型的操作创建实现。 由于我们有三个动作,因此我们将创建三个Command实现。 每个Command实现都会将请求转发到适当的接收方方法。

package com.journaldev.design.command;public class OpenFileCommand implements Command {private FileSystemReceiver fileSystem;public OpenFileCommand(FileSystemReceiver fs){this.fileSystem=fs;}@Overridepublic void execute() {//open command is forwarding request to openFile methodthis.fileSystem.openFile();}}
package com.journaldev.design.command;public class CloseFileCommand implements Command {private FileSystemReceiver fileSystem;public CloseFileCommand(FileSystemReceiver fs){this.fileSystem=fs;}@Overridepublic void execute() {this.fileSystem.closeFile();}}
package com.journaldev.design.command;public class WriteFileCommand implements Command {private FileSystemReceiver fileSystem;public WriteFileCommand(FileSystemReceiver fs){this.fileSystem=fs;}@Overridepublic void execute() {this.fileSystem.writeFile();}}

Now we have receiver and command implementations ready, so we can move to implement the invoker class.

现在我们已经准备好接收器和命令实现,因此可以继续执行调用程序类。

命令模式调用者类 (Command Pattern Invoker Class)

Invoker is a simple class that encapsulates the Command and passes the request to the command object to process it.

Invoker是一个简单的类,它封装Command,并将请求传递给Command对象以对其进行处理。

package com.journaldev.design.command;public class FileInvoker {public Command command;public FileInvoker(Command c){this.command=c;}public void execute(){this.command.execute();}
}

Our file system utility implementation is ready and we can move to write a simple command pattern client program. But before that I will provide a utility method to create the appropriate FileSystemReceiver object.

我们的文件系统实用程序实现已经准备就绪,我们可以继续编写简单的命令模式客户端程序。 但是在此之前,我将提供一种实用程序方法来创建适当的FileSystemReceiver对象。

Since we can use System class to get the operating system information, we will use this or else we can use Factory pattern to return appropriate type based on the input.

由于我们可以使用System类来获取操作系统信息 ,因此将使用此类,否则我们可以使用Factory模式来基于输入返回适当的类型。

package com.journaldev.design.command;public class FileSystemReceiverUtil {public static FileSystemReceiver getUnderlyingFileSystem(){String osName = System.getProperty("os.name");System.out.println("Underlying OS is:"+osName);if(osName.contains("Windows")){return new WindowsFileSystemReceiver();}else{return new UnixFileSystemReceiver();}}}

Let’s move now to create our command pattern example client program that will consume our file system utility.

现在开始创建将使用我们的文件系统实用程序的命令模式示例客户端程序。

package com.journaldev.design.command;public class FileSystemClient {public static void main(String[] args) {//Creating the receiver objectFileSystemReceiver fs = FileSystemReceiverUtil.getUnderlyingFileSystem();//creating command and associating with receiverOpenFileCommand openFileCommand = new OpenFileCommand(fs);//Creating invoker and associating with CommandFileInvoker file = new FileInvoker(openFileCommand);//perform action on invoker objectfile.execute();WriteFileCommand writeFileCommand = new WriteFileCommand(fs);file = new FileInvoker(writeFileCommand);file.execute();CloseFileCommand closeFileCommand = new CloseFileCommand(fs);file = new FileInvoker(closeFileCommand);file.execute();}}

Notice that client is responsible to create the appropriate type of command object. For example if you want to write a file you are not supposed to create CloseFileCommand object.

注意,客户端负责创建适当类型的命令对象。 例如,如果要编写文件,则不应创建CloseFileCommand对象。

Client program is also responsible to attach receiver to the command and then command to the invoker class.

客户端程序还负责将接收方附加到命令,然后将命令附加到调用方类。

Output of the above command pattern example program is:

上面的命令模式示例程序的输出为:

Underlying OS is:Mac OS X
Opening file in unix OS
Writing file in unix OS
Closing file in unix OS

命令模式类图 (Command Pattern Class Diagram)

Here is the class diagram for our file system utility implementation.

这是我们的文件系统实用程序实现的类图。

命令模式要点 (Command Pattern Important Points)

  • Command is the core of command design pattern that defines the contract for implementation.命令是命令设计模式的核心,它定义了实现合同。
  • Receiver implementation is separate from command implementation.接收器实现与命令实现是分开的。
  • Command implementation classes chose the method to invoke on receiver object, for every method in receiver there will be a command implementation. It works as a bridge between receiver and action methods.命令实现类选择了要在接收器对象上调用的方法,因为接收器中的每个方法都会有一个命令实现。 它充当接收方和操作方法之间的桥梁。
  • Invoker class just forward the request from client to the command object.Invoker类仅将来自客户端的请求转发到命令对象。
  • Client is responsible to instantiate appropriate command and receiver implementation and then associate them together.客户负责实例化适当的命令和接收器实现,然后将它们关联在一起。
  • Client is also responsible for instantiating invoker object and associating command object with it and execute the action method.客户端还负责实例化调用者对象并将命令对象与其关联,并执行action方法。
  • Command design pattern is easily extendible, we can add new action methods in receivers and create new Command implementations without changing the client code.命令设计模式易于扩展,我们可以在接收器中添加新的操作方法并创建新的Command实现,而无需更改客户端代码。
  • The drawback with Command design pattern is that the code gets huge and confusing with high number of action methods and because of so many associations.Command设计模式的缺点在于,由于大量的关联,代码变得庞大且与大量的操作方法混淆。

命令设计模式JDK示例 (Command Design Pattern JDK Example)

Runnable interface (java.lang.Runnable) and Swing Action (javax.swing.Action) uses command pattern.

Runnable接口 (java.lang.Runnable)和Swing Action(javax.swing.Action)使用命令模式。

翻译自: https://www.journaldev.com/1624/command-design-pattern

设计模式 指令模式

设计模式 指令模式_指令设计模式相关推荐

  1. 设计模式 原型模式_创新设计模式:原型模式

    设计模式 原型模式 原型模式用于创建对象的副本. 这种模式非常有用,特别是当从头开始创建对象的成本很高时. 与builder , factory和abstract factory模式相比,它不会从头开 ...

  2. java设计模式迭代器模式_迭代器设计模式示例

    java设计模式迭代器模式 本文是我们名为" Java设计模式 "的学院课程的一部分. 在本课程中,您将深入研究大量的设计模式,并了解如何在Java中实现和利用它们. 您将了解模式 ...

  3. 设计模式 原型模式_原型设计模式:创建另一个小车

    设计模式 原型模式 创建对象确实是一个耗时的过程,也是一件昂贵的事情. 因此,我们现在正冒险节省时间和金钱. 我们该怎么做? 克隆奇迹多莉 有人记得多莉吗? 是的,是绵羊,是第一个被克隆的哺乳动物. ...

  4. 备忘录模式 命令模式_备忘录设计模式示例

    备忘录模式 命令模式 本文是我们名为" Java设计模式 "的学院课程的一部分. 在本课程中,您将深入研究大量的设计模式,并了解如何在Java中实现和利用它们. 您将了解模式如此重 ...

  5. 设计模式 原型模式_设计模式:原型

    设计模式 原型模式 创新设计模式之一是原型设计模式 . 尽管原型是创造模式,但它在概念上与其他模式有所区别. 我的意思是原型在某种意义上创造了自己. 我将在下面解释. 原型模式的所有魔力都基于Java ...

  6. 设计模式 建造者模式_设计模式:建造者

    设计模式 建造者模式 有时需要在应用程序中创建一个复杂的对象. 一种解决方案是Factory模式,另一种是Builder设计模式. 在某些情况下,您甚至可以结合使用这两种模式. 但是在本文中,我想研究 ...

  7. java设计模式迭代器模式_迭代器模式和Java

    java设计模式迭代器模式 大家好,在本文中,我们将检查Iterator Pattern . 我知道你们中许多人已经使用过一种设计模式,但是也许您没有意识到它是模式,或者不知道它的巨大价值. 根据&l ...

  8. 设计模式行为模式_使用行为模式建立很棒的社区

    设计模式行为模式 人类是复杂的动物. 我们充满了野心,恐惧,欲望,焦虑和其他人类状况. 当然,这些不同元素的范围和表现因人而异,在不同文化之间,在不同环境中也不同. 这使构建人类系统(例如社区或公司) ...

  9. 设计模式 工厂方法_工厂设计模式–一种有效的方法

    设计模式 工厂方法 如您所知,"工厂方法模式"或俗称"工厂设计模式"是"创意设计模式"类别下的一种设计模式. 模式背后的基本原理是,在运行时 ...

最新文章

  1. python上机实验报告读取文件_Python程序设计实验报告:实验八 文件
  2. linux日志显示too many open files解决
  3. 《研磨设计模式》chap21 解释器模式Interpreter(1)模式介绍
  4. 如何在C#控件中画点并获得指定点的像素颜色
  5. ObjectTive C语言语法,[译]理解 Objective-C 运行时(下篇)
  6. VS2010中预处理器定义
  7. python代码导出_代码生成 – Python生成Python
  8. excel的mysql语言,Mysql中文乱码及导出sql语句和Excel的相关解决方法
  9. 99+好友共同关注,公众号推荐
  10. smart-framework框架学习之DispatcherServlet分发机制
  11. JMeter Exception: java.net.BindException: Address already in use: connect(转)
  12. Advanced Installer 历史版本下载
  13. HmacSHA256算法实现消息认证
  14. java end_Java Matcher end()用法及代码示例
  15. 腾讯云买服务器密码,腾讯云服务器初始密码是什么?
  16. HDU 6514 2019中山大学程序设计竞赛(二维前缀和)
  17. 计算机课程设计心得,课程设计心得体会450字
  18. Android10修改电池图标,导航栏、信号及电池图标修改方法(新增视频教程)
  19. 给 木子健康管理室 添加微信公众号 并制作一条 图文消息
  20. Saber 2016安装过程分享

热门文章

  1. [原]LVM管理与虚拟机管理
  2. android 之反编译
  3. Oracle性能优化技巧
  4. 使用 Django 的日志模块,同时发送错误邮件到163邮箱
  5. [转载] 比较器(Comparable和Comparator)、自然排序、定制排序
  6. UCML 参与者关键 与依赖关联外键
  7. 冒泡排序的一次自我救赎
  8. Linux系统上安装JDK和Tomcat服务器
  9. 【习题 3-1 UVA - 1585】Score
  10. 接收POst数据流数据