Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Command pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Terminology== The terminology used to describe command pattern implementations is not consistent and can therefore be confusing. This is the result of [[ambiguity]], the use of [[synonyms]], and implementations that may obscure the original pattern by going well beyond it. # Ambiguity. ## The term '''command''' is ambiguous. For example, ''move up, move up'' may refer to a single (move up) command that should be executed twice, or it may refer to two commands, each of which happens to do the same thing (move up). If the former command is added twice to an undo stack, both items on the stack refer to the same command instance. This may be appropriate when a command can always be undone the same way (e.g. move down). Both the [[Gang of Four (software)|Gang of Four]] and the [[#Java|Java example below]] use this interpretation of the term ''command''. On the other hand, if the latter commands are added to an undo stack, the stack refers to two separate objects. This may be appropriate when each object on the stack must contain information that allows the command to be undone. For example, to undo a ''delete selection'' command, the object may contain a copy of the deleted text so that it can be re-inserted, if the ''delete selection'' command must be undone. Note that using a separate object for each invocation of a command is also an example of the [[chain of responsibility pattern]]. ## The term '''execute''' is also ambiguous. It may refer to running the code identified by the command object's ''execute'' method. However, in Microsoft's [[Windows Presentation Foundation]] a command is considered to have been executed when the command's ''execute'' method has been invoked, but that does not necessarily mean that the application code has run. That occurs only after some further event processing. # Synonyms and [[homonyms]]. ## '''Client, Source, Invoker''': the button, toolbar button, or menu item clicked, the shortcut key pressed by the user. ## '''Command Object, Routed Command Object, Action Object''': a singleton object (e.g. there is only one <code>CopyCommand</code> object), which knows about shortcut keys, button images, command text, etc. related to the command. A source or invoker object calls the Command or Action object's execute or performAction method. The Command/Action object notifies the appropriate source/invoker objects when the availability of a command/action has changed. This allows buttons and menu items to become inactive (grayed out) when a command/action cannot be executed/performed. ## '''Receiver, Target Object''': the object that is about to be copied, pasted, moved, etc. The receiver object owns the method that is called by the command's ''execute'' method. The receiver is typically also the target object. For example, if the receiver object is a ''cursor'' and the method is called <code>moveUp</code>, then one would expect that the cursor is the target of the <code>moveUp</code> action. On the other hand, if the code is defined by the command object itself, the target object will be a different object entirely. ## '''Command Object, routed event arguments, event object''': the object that is passed from the source to the Command/Action object, to the Target object to the code that does the work. Each button click or shortcut key results in a new command/event object. Some implementations add more information to the command/event object as it is being passed from one object (e.g. <code>CopyCommand</code>) to another (e.g. document section). Other implementations put command/event objects in other event objects (like a box inside a bigger box) as they move along the line, to avoid naming conflicts. (See also [[chain of responsibility pattern]].) ## '''Handler, <code>ExecutedRoutedEventHandler</code>, method, function''': the actual code that does the copying, pasting, moving, etc. In some implementations the handler code is part of the command/action object. In other implementations the code is part of the Receiver/Target Object, and in yet other implementations the handler code is kept separate from the other objects. ## '''Command Manager, Undo Manager, Scheduler, Queue, Dispatcher, Invoker''': an object that puts command/event objects on an undo stack or redo stack, or that holds on to command/event objects until other objects are ready to act on them, or that routes the command/event objects to the appropriate receiver/target object or handler code. # Implementations that go well beyond the original command pattern. ## Microsoft's [http://msdn.microsoft.com/en-us/library/ms752308.aspx Windows Presentation Foundation] (WPF), introduces routed commands, which combine the command pattern with event processing. As a result, the command object no longer contains a reference to the target object nor a reference to the application code. Instead, invoking the command object's ''execute'' command results in a so-called ''Executed Routed Event'' that during the event's tunneling or bubbling may encounter a so-called ''binding'' object that identifies the target and the application code, which is executed at that point.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)