CSC/ECE 517 Fall 2010/ch4 4f sv: Difference between revisions
Line 113: | Line 113: | ||
== Rules of thumb == | == Rules of thumb[http://sourcemaking.com/design_patterns/command] == | ||
*[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility], Command, [http://en.wikipedia.org/wiki/Mediator_pattern Mediator], and [http://en.wikipedia.org/wiki/Observer_pattern Observer], address how you can decouple senders and receivers, but with different trade-offs. *Command normally specifies a sender-receiver connection with a subclass. | *[http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern Chain of Responsibility], Command, [http://en.wikipedia.org/wiki/Mediator_pattern Mediator], and [http://en.wikipedia.org/wiki/Observer_pattern Observer], address how you can decouple senders and receivers, but with different trade-offs. *Command normally specifies a sender-receiver connection with a subclass. | ||
Line 122: | Line 122: | ||
*A Command that must be copied before being placed on a history list acts as a Prototype. | *A Command that must be copied before being placed on a history list acts as a Prototype. | ||
*Two important aspects of the Command pattern: interface separation (the invoker is isolated from the receiver), time separation (stores a ready-to-go processing request that’s to be stated later). | *Two important aspects of the Command pattern: interface separation (the invoker is isolated from the receiver), time separation (stores a ready-to-go processing request that’s to be stated later). | ||
== Example of Command pattern == | == Example of Command pattern == |
Revision as of 21:43, 3 October 2012
Command Pattern
What is Command Pattern ?
In object-oriented programming, the command pattern is a 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. It encapsulates a request as an object and gives it a known public interface. It ensures that every object receives its own commands and provides a decoupling between sender and receiver. In this case a sender is an object that invokes an operation, and a receiver is an object that receives the request and acts on it.
The Command pattern has three main components: the Invoker, the Command, and the Receiver. The invoker component acts as a link between the commands and the receiver, and houses the receiver and the individual commands as they are sent. The command is an object that encapsulates a request to the receiver. The receiver is the component that is acted upon by each request.
The intent of using command objects is to easily construct the general components that need to delegate, sequence or execute method calls at a time of their choosing without the need to know the owner of the method or the method parameters. Command design pattern provides the options to queue commands, undo/redo actions and other manipulations.
Developers might use a switch statement with a case for each command. Usage of Switch statements during coding is a sign of bad design during the design phase of an object-oriented project. Commands represent an object-oriented way to support transactions and can be used to solve this design problem.
Why and When do we use a Command Pattern?
Command Pattern comes into picture when there is a need to store objects behaviour as a command and its state. The Command pattern encapsulates commands (method calls) in objects allowing us to issue requests without knowing the requested operation or the requesting object. Command pattern provides the options to queue commands, undo/redo actions and other manipulations.
There are several benefits[1] of using the Command pattern:
- It provides encapsulation of application logic so that it can be executed at a different point of time.
- It allows to execute the application in separate contexts, such as in a different thread or using a different state by separating the application logic and context.
- The separation between application logic and context allows to easier exchange the application logic.
Command Patterns can be used for the following:
Undo and redo actions
Some implementations of the Command design pattern include parts for supporting undo and redo of actions. In order to do that a mechanism to obtain past states of the Receiver object is needed; in order to achieve this there are two options:
- Before running each command a snapshot of the receiver state is stored in memory. This does not require much programming effort but can not be always applied. For example doing this in an image processing application would require storing images in memory after each step, which is practically impossible.
- Instead of storing receiver objects states, the set of performed operations are stored in memory. In this case the command and receiver classes should implement the inverse algorithms to undo each action. This will require additional programming effort, but less memory will be required. Sometimes for undo/redo actions the command should store more information about the state of the receiver objects. A good idea in such cases is to use the Memento Pattern.
Asynchronous Method Invocation
Another usage for the command design pattern is to run commands asynchronous in background of an application. In this case the invoker is running in the main thread and sends the requests to the receiver which is running in a separate thread. The invoker will keep a queue of commands to be run and will send them to the receiver while it finishes running them.
Instead of using one thread in which the receiver is running more threads can be created for this. But for performance issues (thread creation is consuming) the number of threads should be limited. In this case the invoker will use a pool of receiver threads to run command asynchronously.
Adding new commands
The command object decouples the object that invokes the action from the object that performs the action. There are implementations of the pattern in which the invoker instantiates the concrete command objects. In this case if we need to add a new command type we need to change the invoker as well. And this would violate the Open Close Principle (OCP). In order to have the ability to add new commands with minimum of effort we have to make sure that the invoker is aware only about the abstract command class or interface.
Using composite commands
When adding new commands to the application we can use the composite pattern to group existing commands in another new command. This way, macros can be created from existing commands.
Applications
Command objects are useful for implementing:
- GUI buttons and menu items
- In Swing and Borland Delphi programming, an Action is a command object. In addition to the ability to perform the desired command, an
Action
may have an associated icon, keyboard shortcut, tooltip text, and so on. A toolbar button or menu item component may be completely initialized using only theAction
object. - Macro recording
- If all user actions are represented by command objects, a program can record a sequence of actions simply by keeping a list of the command objects as they are executed. It can then "play back" the same actions by executing the same command objects again in sequence. If the program embeds a scripting engine, each command object can implement a toScript() method, and user actions can then be easily recorded as scripts.
- Mobile Code
- Using languages such as Java where code can be streamed/slurped from one location to another via URLClassloaders and Codebases the commands can enable new behavior to be delivered to remote locations (EJB Command, Master Worker)
- Multi-level undo
- If all user actions in a program are implemented as command objects, the program can keep a stack of the most recently executed commands. When the user wants to undo a command, the program simply pops the most recent command object and executes its undo() method.
- Networking
- It is possible to send whole command objects across the network to be executed on the other machines, for example player actions in computer games.
- Parallel Processing
- Where the commands are written as tasks to a shared resource and executed by many threads in parallel (possibly on remote machines -this variant is often referred to as the Master/Worker pattern)
- Thread pools
- A typical, general-purpose thread pool class might have a public
addTask()
method that adds a work item to an internal queue of tasks waiting to be done. It maintains a pool of threads that execute commands from the queue. The items in the queue are command objects. Typically these objects implement a common interface such asjava.lang.Runnable
that allows the thread pool to execute the command even though the thread pool class itself was written without any knowledge of the specific tasks for which it would be used. - Transactional behavior
- Similar to undo, a database engine or software installer may keep a list of operations that have been or will be performed. Should one of them fail, all others can be reverted or discarded (usually called rollback). For example, if two database tables that refer to each other must be updated, and the second update fails, the transaction can be rolled back, so that the first table does not now contain an invalid reference.
- Wizards
- Often a wizard presents several pages of configuration for a single action that happens only when the user clicks the "Finish" button on the last page. In these cases, a natural way to separate user interface code from application code is to implement the wizard using a command object. The command object is created when the wizard is first displayed. Each wizard page stores its GUI changes in the command object, so the object is populated as the user progresses. "Finish" simply triggers a call to execute(). This way, the command class contains no user interface code.
Components of a command pattern
The various components of a command pattern[2] are given as follows:
Client: Invokes a particular module using a command and passes a request which gets propagated as a command.
Command: It is an object that encapsulates a request to the receiver. Command request maps to particular modules. According to the command, a module is invoked.
Receiver: It is the component that is acted upon by each request. It knows how to perform the operations associated with carrying out a certain request.
Invoker: It is the class that is invoked by the client. It takes in the request and calls the receiver by passing the command to it and asks it to carry out the request.
Concrete Command: It defines a link between the receiver and the action. It basically implements the execute method and invokes the corresponding operations on the receiver.
Throughout this page, we will discuss the example of an operation of converting a string into either upper case or lower case.
The components corresponding to the example are given in the figure above. From the figure we can identify the components as follows:
Client: Menu Class is used to set the ConcreteCommand and then sets the convert class
Command: Command Class in this case is declaring the interface which is used to execute the operation.
Receiver: Convert Class is the one which knows how to perform the operations.
Invoker: TextOperation Class asks the command to carry out the action which in this case is to either convert toUpper or toLower.
ConcreteCommand: ConvertUpper, ConvertLower classes are the ones that implement the execute command.
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 and the 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 CopyCommand object), which knows about shortcut keys, button images, command text, etc. related to the command. A source/invoker object calls the Command/Action object's execute/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 moveUp, then one would expect that the cursor is the target of the moveUp 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 args, 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. CopyCommand) 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, ExecutedRoutedEventHandler, 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 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 which 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.
Check list for Command Pattern Implementation
- Define a Command interface with a method signature like
execute()
. - Create one or more derived classes that encapsulate some subset of the following: a “receiver” object, the method to invoke, the arguments to pass.
- Instantiate a Command object for each deferred execution request.
- Pass the Command object from the creator (aka sender) to the invoker (aka receiver).
- The invoker decides when to
execute()
.
Rules of thumb[3]
- Chain of Responsibility, Command, Mediator, and Observer, address how you can decouple senders and receivers, but with different trade-offs. *Command normally specifies a sender-receiver connection with a subclass.
- Chain of Responsibility can use Command to represent requests as objects.
- Command and Memento act as magic tokens to be passed around and invoked at a later time. In Command, the token represents a request; in *Memento, it represents the internal state of an object at a particular time. Polymorphism is important to Command, but not to Memento because its interface is so narrow that a memento can only be passed as a value.
- Command can use Memento to maintain the state required for an undo operation.
- MacroCommands can be implemented with Composite.
- A Command that must be copied before being placed on a history list acts as a Prototype.
- Two important aspects of the Command pattern: interface separation (the invoker is isolated from the receiver), time separation (stores a ready-to-go processing request that’s to be stated later).
Example of Command pattern
Consider the case of converting a string to upper and lower case.The Client is the Menu where we see the options. The menu sends the request to the receiver(convert class) through the Invoker(TextOperations class). The textOperation encapsulates the command and then forwards it, creating the ConcreteCommand object which is the command itself. The Receiver will be the Convert class that, after completing all the commands that were sent to it before the command in question(to Upper case or to lower case ), starts to work on it.
Implementation in static and dynamic languages
Implementation in Static languages
We initially define a Command interface with a method named execute() and then we create command classes that implement this interface. The execute() method of each object will contain the logic specific to the action you are encapsulating. (For instance, you might create a convertUpper and a converLower command in a text editor application.)
In case of the static implementation, code snippets to implement the command pattern in languages like Java and C# are given as follows:
Java Example
//This is the command interface. public interface Command { void execute(); } //A command to convert to lower case. public class convertLower implements Command { private Convert convert; private convertLower(Convert conv) { this.convert = conv; } public void execute() { convert.toLower(); } } //A command to convert to upper case public class convertUpper implements Command { private Convert convert; private convertUpper(Convert conv) { this.convert = conv; } public void execute() { convert.toUpper(); } } //This is the reciever class. public class Convert { public Convert() { } public final void toUpper() { System.out.print("Turning all lower case elements to upper case");//The actual action that is being performed. } public final void toLower() { System.out.print("Converting all upper case elements to lower case");//The actual action that is being performed. } } // The invoker class public class TextOperations { private Command convertUp; private Command convertLow; private TextOperations(Command toup, Command todwn) { this.convertUp = toup; this.convertLow = todwn; } public void convToUp() { convertUp.execute();//The execute method that calls the execute method in the interface } public void convToLow() { convertLow.execute();//The execute method that calls the execute method in the interface } } // This is the client class. public class Menu { public static void main(String args[]) { Convert c = new Convert(); Command cou = new convertUpper(c); Command col = new convertLower(c); TextOperations converter = new TextOperations(cou,col); /*Client calls the invoker and gives the commands to execute*/ converter.convToLow(); converter.convToUp(); } }
C# Example
//This is the command interface. public interface Command { void execute(); //The execute method that is going to call the action } //A command to convert to lower case. public class convertLower : Command { private Convert convert; private convertLower(Convert conv) { this.convert = conv; } public void execute() { convert.toLower(); } } //A command to convert to upper case public class convertUpper : Command { private Convert convert; private convertUpper(Convert conv) { this.convert = conv; } public void execute() { convert.toUpper(); } } //This is the reciever class. public class Convert { public Convert() { } public final void toUpper() { Console.WriteLine("Turning all lower case elements to upper case");// These are the actual actions that are performed } public final void toLower() { Console.WriteLine("Converting all upper case elements to lower case"); //These is the actual action that is performed } } // The invoker class public class TextOperations { private Command convertUp; private Command convertLow; private TextOperations(Command toup, Command todwn) { this.convertUp = toup; this.convertLow = todwn; } public virtual void convToUp() { convertUp.execute(); } public virtual void convToLow() { convertLow.execute(); } } // This is the client class. public class Menu { Static void Main(string[] args) { Convert c = new Convert(); Command cou = new convertUpper(c); Command col = new convertLower(c); TextOperations converter = new TextOperations(cou,col); /*Client calls the invoker and gives the commands to execute*/ converter.convToLow(); converter.convToUp(); } }
Implementation in Dynamic Languages
In dynamic languages we will have a controller class that constructs each of the Command objects.Each of these command objects is passed to an invoker class. The invoker class calls the execute() method of a Command object at the appropriate time. (For instance this may happen when a user selects a menu item or presses a button)
In case of the dynamic languages, the code snippets to implement command pattern for ruby and python are given as follows.
Ruby Example
#This is the command interface. class Command def execute() end end #A command to convert to lower case. class convertLower < Command def initialize(conv) self.@convert = conv end def execute() @convert.toLower() end end #A command to convert to upper case class convertUpper < Command def initialize(conv) self.@convert = conv end def execute() @convert.toUpper() end end #This is the reciever class. class Convert def initialize() end def toUpper() Console.WriteLine("Turning all lower case elements to upper case") end def toLower() Console.WriteLine("Converting all upper case elements to lower case") end end # The invoker class class TextOperations def initialize(toup, todwn) self.@convertUp = toup self.@convertLow = todwn end def convToUp() @convertUp.execute() end def convToLow() @convertLow.execute() end end # This is the client class. class Menu def Main(args) c = Convert.new() cou = convertUpper.new(c) col = convertLower.new(c) converter = TextOperations.new(cou, col) # Client calls the invoker and gives the commands to execute converter.convToLow() converter.convToUp() end end
Python Example
#This is the command interface. class Command(object): def execute(self): pass #A command to convert to lower case. class convertLower(Command): def __init__(self, conv): self._convert = conv def execute(self): self._convert.toLower() #A command to convert to upper case class convertUpper(Command): def __init__(self, conv): self._convert = conv def execute(self): self._convert.toUpper() #This is the reciever class. class Convert(object): def __init__(self): pass def toUpper(self): Console.WriteLine("Turning all lower case elements to upper case") def toLower(self): Console.WriteLine("Converting all upper case elements to lower case") # The invoker class class TextOperations(object): def __init__(self, toup, todwn): self._convertUp = toup self._convertLow = todwn def convToUp(self): self._convertUp.execute() def convToLow(self): self._convertLow.execute() # This is the client class. class Menu(object): def Main(self, args): c = Convert() cou = convertUpper(c) col = convertLower(c) converter = TextOperations(cou, col) # Client calls the invoker and gives the commands to execute converter.convToLow() converter.convToUp()
Static vs Dynamic implementations
In case of the implementations ruby has better implementation of the command patterns than the languages like Java. Some of the advantages of Ruby over Java can be given as follows.
- To implement the command pattern in static languages like Java, we have to define a common interface which has to be extended by all the other classes. In case of ruby we can use procs which makes it easier to implement.
- Ruby implements something called mixins in order to implement the command pattern which is efficient compared to Java.
- The lines of code for static language like Java is more than that taken by the dynamic language like Ruby. This is more because of the proc objects that are present in ruby which are concisely defined than that present in Java.
- Since Ruby is a dynamic language, one can add the commands to the command array during runtime. Java is a static language and hence does not support this feature.
Conclusion
Hence it can be summarized that the dynamically typed languages keep the code straight and narrow as it does not need to depend on static type-checking. Also due to the flexibility of the dynamic languages, writing code is significantly easier.
References
[1] Freeman,E., and Robson,E.,and Bates,B.,and Sierra,K. 2004. Head First Design Patterns
[2] Wikipedia. (2010, October) Wikipedia - Command_Pattern. [Online]. http://en.wikipedia.org/wiki/Command_pattern
[3] Oodesign. (2005-2006)- command-pattern. [Online]. http://www.oodesign.com/command-pattern.html
[4] Sourcemaking. - Command design-pattern. [Online]. http://sourcemaking.com/design_patterns/command
[6] Erich,G., and Richard,H.,and Ralph,J.,and John,M.V. 1997. Design Patterns: Elements of Reusable Object-Oriented Software
[7] McDonald,J. 2008. Design Patterns
[8] James,C., 1998. The Design Patterns-Java Companion. [Online]. http://www.patterndepot.com/put/8/command.pdf