原版设计模式之命令模式

Intent(定义)

Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.(将请求封装为一个对象,允许你使用不同的请求、队列或日志请求来作为客户端的参数,并支持不可执行的操作。

Also Known As (别名)

Action, Transaction

Motivation (案例)

The Command pattern lets toolkit objects make requests of unspecified application objects by turning the request itself into an object. This object can be stored and passed around like other objects. The key to this pattern is an abstract Command class, which declares an interface for executing operations. In the simplest form this interface includes an abstract Execute operation. Concrete Command subclasses specify a receiver-action pair by storing the receiver as an instance variable and by implementing Execute to invoke the request. The receiver has the knowledge required to carry out the request.(命令模式允许工具包对象通过将请求转换为一个对象来请求未指定的应用程序对象。这个对象可以像其他对象一样被存储和传递。这个模式的关键是一个抽象的Command类,它声明了一个用于执行操作的接口。在最简单的形式中,这个接口包括一个抽象的Execute 方法。具体的Command 子类通过将receiver存储为实例变量来指定一个配对的接收者行为。receiver具备执行请求所需的条件。
在这里插入图片描述
Menus can be implemented easily with Command objects. Each choice in a Menu is an instance of a MenuItem class. An Application class creates these menus and their menu items along with the rest of the user interface. The Application class also keeps track of Document objects that a user has opened.(菜单可以通过Command 对象轻松实现。Menu 中的每个选项都是一个MenuItem 类的实例。Application 类将创建这些菜单及其菜单项以及用户界面的其他部分。Application 类还会跟踪用户已打开的Document 对象。

The application configures each MenuItem with an instance of a concrete Command subclass. When the user selects a MenuItem, the MenuItem calls Execute on its command, and Execute carries out the operation. MenuItems don’t know which subclass of Command they use. Command subclasses store the receiver of the request and invoke one or more operations on the receiver.(应用程序使用具体Command 子类的实例配置每个MenuItem 。当用户选择菜单项时,MenuItem 调用Command的Execute方法,并执行操作。菜单项不知道他们使用的是哪个Command 的子类。Command 子类存储请求的接收器,并在接收器上调用一个或多个操作。

For example, PasteCommand supports pasting text from the clipboard into a Document. (例如,PasteCommand 支持将剪贴板中的文本粘贴到文档中。
PasteCommand’s receiver is the Document object it is supplied upon instantiation. (PasteCommand 的接收方是在PasteCommand 实例化时提供的Document对象。
The Execute operation invokes Paste on the receiving Document.(Execute 方法将调用Document的Paste方法。
在这里插入图片描述

OpenCommand’s Execute operation is different: it prompts the user for a document name, creates a corresponding Document object, adds the document to the receiving application, and opens the document.(OpenOcommand的执行操作不同:它提示用户输入文档名称,创建相应的Document 对象,将文档添加到接收应用程序,然后打开文档。
在这里插入图片描述

Sometimes a MenuItem needs to execute a sequence of commands. For example, a MenuItem for centering a page at normal size could be constructed from a CenterDocumentCommand object and a NormalSizeCommand object.(有时,一个MenuItem 需要执行一系列的命令。例如,用于在正常大小的页面居中的菜单项 ,可以由CenterDocumentCommand 对象和NormalSizeCommand 对象构建而成。) Because it’s common to string commands together in this way, we can define a MacroCommand class to allow a MenuItem to execute an open-ended number of commands. MacroCommand is a concrete Command subclass that simply executes a sequence of Commands. MacroCommand has no explicit receiver, because the commands it sequences define their own receiver.(因为以这种方式组合字符串命令是很常见的,所以我们可以定义一个MacroCommand 类来允许MenuItem 执行无限数量的命令。MacroCommand 是一个具体的命令子类,它只是简单地执行一系列命令。MacroCommand 没有显式的接收器,因为这一系列的命令定义了它们自己的接收器。
在这里插入图片描述
In each of these examples, notice how the Command pattern decouples the object that invokes the operation from the one having the knowledge to perform it. This gives us a lot of flexibility in designing our user interface.(在这些示例中,注意命令模式如何将调用操作的对象与具有执行操作的对象解耦。这让我们在设计用户界面时有了很大的灵活性。) An application can provide both a menu and a push button interface to a feature just by making the menu and the push button share an instance of the same concrete Command subclass. We can replace commands dynamically, which would be useful for implementing context-sensitive menus.(应用程序只要使菜单和按钮共享相同的具体Command 子类的实例,就可以为一个功能提供一个菜单和一个按钮界面。我们可以动态地替换命令,这对于实现对上下文敏感的菜单非常有用。

We can also support command scripting by composing commands into larger ones. All of this is possible because the object that issues a request only needs to know how to issue it; it doesn’t need to know how the request will be carried out.(我们还可以通过将命令组合成更大的命令来支持命令脚本编写。因为发出请求的对象只需要知道如何发出它;它不需要知道请求将如何执行。

Applicability (适用点)

  • parameterize objects by an action to perform, as MenuItem objects did above. You can express such parameterization in a procedural language with a callback function, that is, a function that’s registered somewhere to be called at a later point. Commands are an object-oriented replacement for callbacks.(通过把要执行的操作变成参数,就像上面的MenuItem 对象所做的那样。你可以在程序中用回调函数来表示这种参数化,也就是说,一个功能先定义,随后会被传递到方法中,在方法内部执行。命令是对回调的一种面向对象的替代品。
  • specify, queue, and execute requests at different times. A Command object can have a lifetime independent of the original request. If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there.(在不同的时间指定、设置队列和执行请求。Command 对象可以具有独立于原始请求的生命周期。如果请求的接收方以独立地址空间的方式表示,那么你可以将请求的命令对象传输到另一个不同的进程,并在那里满足请求。【没看懂这段】
  • support undo. The Command’s Execute operation can store state for reversing its effects in the command itself. The Command interface must have an added Unexecute operation that reverses the effects of a previous call to Execute. Executed commands are stored in a history list. Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling Unexecute and Execute, respectively.(支持撤消。命令的执行操作可以存储状态,用于在命令中撤销其产生的效果。命令界面必须有一个额外的撤销操作,以撤销先前调用执行的效果。已执行的命令将存储在一个历史记录列表中。通过来回遍历此列表,分别调用撤销和执行,来实现无限的撤销和重做。【此处应该是向前和向后回退】
  • support logging changes so that they can be reapplied in case of a system crash. By augmenting the Command interface with load and store operations, you can keep a persistent log of changes. Recovering from a crash involves reloading logged commands from disk and reexecuting them with the Execute operation.(支持日志记录更改,以便在发生系统崩溃时可以重新应用这些更改。通过使用加载和存储操作增强命令界面,让你可以持久性的记录变化。通过从磁盘中重新加载记录的命令,并重新执行这些命令,将崩溃的系统恢复。
  • structure a system around high-level operations built on primitives operations. Such a structure is common in information systems that support transactions. A transaction encapsulates a set of changes to data. The Command pattern offers a way to model transactions. Commands have a common interface, letting you invoke all transactions the same way. The pattern also makes it easy to extend the system with new transactions.(建立在原始操作基础上用高级操作构建一个系统。这种结构在支持事务的信息系统中很常见。事务封装了对数据的更改。命令模式提供了一种对事务构建模型的方法。命令有一个公共接口,允许您以相同的方式调用所有事务。该模式还使使用新的事务来扩展系统变得很容易。

Structure (结构)

在这里插入图片描述

Participants (参与者)

  • Command(命令类
    • declares an interface for executing an operation(声明一个用于执行操作的接口
  • ConcreteCommand (PasteCommand, OpenCommand) (具体命令
    • defines a binding between a Receiver object and an action. (义了一个接收对象和一个操作之间的绑定
    • implements Execute by invoking the corresponding operation(s) on Receiver.(通过调用接收对象上相应的操作来实现Execute 方法。
  • Client (Application)
    • creates a ConcreteCommand object and sets its receiver.(建一个ConcreteCommand 对象,并设置其接收对象。
  • Invoker (MenuItem) (调用者
    • asks the command to carry out the request.(请求命令执行请求。
  • Receiver (Document, Application) (接收对象
    • knows how to perform the operations associated with carrying out a request. Any class may serve as a Receiver.(知道如何执行请求相关的操作。任何类都可以作为接收对象。

Collaborations (约定)

  • The client creates a ConcreteCommand object and specifies its receiver.(客户端创建一个ConcreteCommand 对象,并指定其接收者
  • An Invoker object stores the ConcreteCommand object.(调用对象保存了具体的命令对象
  • The invoker issues a request by calling Execute on the command. When commands are undoable, ConcreteCommand stores state for undoing the command prior to invoking Execute.(调用者通过使用命令里的Execute 方法发出请求。当命令无法执行时,ConcreteCommand 在调用Execute之前,会存储未执行前的状态,并撤销执行。
  • The ConcreteCommand object invokes operations on its receiver to carry out the request.(ConcreteCommand 对象调用其接收者的操作来执行请求。
    在这里插入图片描述
    上述此图展示了Command的交互过程。

Related Patterns (相关模式)

A Composite (183) can be used to implement MacroCommands.(复合模式可用于实现宏命令。
A Memento (316) can keep state the command requires to undo its effect.(状态模式可以保存命令撤销所需的状态。
A command that must be copied before being placed on the history list acts as a Prototype (133).(在放入历史记录列表前必须复制命令作为原型(133)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值