Command Design Pattern

2,662 views
2,396 views

Published on

Command Design Pattern - Behavioral Pattern

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,662
On SlideShare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
89
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Command Design Pattern

  1. 1. Command Pattern When Actions Speak Louder Than Words Shahriar Hyder Kaz Software Ltd. 17 th October, 2011
  2. 2. Intent <ul><li>Encapsulates a request as an object, thereby letting you parameterize other objects with different requests, queue or log requests, and support undoable operations </li></ul>
  3. 3. Command Pattern <ul><li>An object is used to represent and encapsulate all the information needed to call a method at a later time </li></ul><ul><li>This information includes the method name, the object that owns the method, and the values for the method parameters </li></ul><ul><li>Three fundamental Command pattern terms: </li></ul><ul><ul><li>Client : instantiates command object and provides information to call the method at a later time </li></ul></ul><ul><ul><li>Invoker : decides which method should be called (Executes the Commands Possibly at a later time) </li></ul></ul><ul><ul><li>Receiver : an instance of the class that contains the method's code (The object the command should affect) </li></ul></ul>
  4. 4. Class Diagram Client Invoker setCommand() <<interface>> Command execute() undo() Receiver action() ConcreteCommand execute() undo() public void execute() { receiver.action() }
  5. 5. Collaborations <ul><li>The Client creates a ConcreteCommand object and specifies its receiver . </li></ul><ul><li>An Invoker object stores the ConcreteCommand object. </li></ul><ul><li>The invoker issues a request by calling execute ( ) on the command. </li></ul><ul><li>The ConcreteCommand object invokes operations on its receiver to carry out the request. </li></ul>
  6. 6. Sequence Diagram aReceiver anInvoker aCommand aClient new Command (Rx) StoreCommand(Command) Execute( ) Action( )
  7. 16. More uses <ul><li>Queuing requests </li></ul><ul><ul><li>Can allocate commands to various threads for processing to load balance between threads/processors </li></ul></ul><ul><li>Logging requests (audit trail) </li></ul><ul><ul><li>Just have to save the command objects as they execute. </li></ul></ul><ul><ul><li>If something goes wrong, we can read the log and re-create the sequence of commands (so no data is lost). </li></ul></ul><ul><ul><li>Can also back out changes that cause troubles </li></ul></ul><ul><li>Command Pattern can model transaction systems </li></ul>
  8. 17. Main Concepts <ul><li>It decouples an object making a request, from the one that knows how to perform it </li></ul><ul><ul><li>the command requester only needs to know how to issue it; it doesn't need to know how to perform it </li></ul></ul><ul><li>Command object is at the center of this decoupling and encapsulates a receiver with an action </li></ul><ul><li>An invoker makes a request of a Command object by calling its execute() method, which invokes those actions on the receiver </li></ul><ul><li>Invokers can be parameterized with Commands, even at runtime </li></ul><ul><li>Macro Commands: an extension of Command that allow multiple commands (Composite) to be invoked </li></ul><ul><li>Two user interfaces may share an instance of the same concrete Command subclass. </li></ul><ul><li>Commands are first-class objects. They can be manipulated and extended like any other object. </li></ul><ul><li>Two important aspects of the Command pattern: 1. interface separation (the invoker is isolated from the receiver), 2. time separation (stores a ready-to-go processing request that’s to be stated later). </li></ul>
  9. 18. Related Patterns! <ul><li>Chain of Responsibility can use Command to represent requests as objects. </li></ul><ul><li>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. </li></ul><ul><li>Memento can hold the state a command requires to undo its effect! </li></ul><ul><li>Composite can be used to implement macro commands! </li></ul><ul><li>Commands that are copied before being placed on a history list act as Prototypes! </li></ul>

×