1 / 10

The Command Pattern

The Command Pattern. Consider the following application. Command Pattern. Problem: We want [a UI] to issue requests to objects without knowing anything… About the request [command] being made. About the receiver [executor] of the request. Solution:

elias
Download Presentation

The Command Pattern

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Command Pattern SE-2811 Dr. Mark L. Hornick

  2. Consider the following application SE-2811 Dr. Mark L. Hornick

  3. Command Pattern Problem: We want [a UI] to issue requests to objects without knowing anything… • About the request [command] being made. • About the receiver [executor] of the request. Solution: Represent the request [command] as an object. • The request [command] can then be stored and passed around like other objects.

  4. Example Configurable GUI represents actions as • Menus and menu items • Push buttons • These objects carry out a request in response to user input, but • The Toolkit cannot implement the request. • Application that uses the toolkit knows what to do. • The Toolkit designers do not know • who will receive the request • the operation that will be carried out as a result.

  5. The Different Players Invoker: UI via Invoker helper Command: CarStartCmd Receiver: Car Execute: startEngine() Invoker: MenuItem Command: PasteCommand, Open Command Receiver: Document, Application Execute(): Calls paste action on the document Calls open action on an application. X10 Example MS-Word Example

  6. Generic Command Pattern UML Class Diagram(Note: from GOF book – UML associations inconsistent w.r.t. current UML standard) Aggregation incorrect Non-std association

  7. Command Pattern Example SE-2811 Dr. Mark L. Hornick

  8. Sequence of Events The client is responsible for creating concrete Command objects The Command objects consists of a set of actions on a Receiver • Which means the Command calls various Receiver methods • The Receiver is also stored in the Command object The client calls an invocation method on an Invoker object and passes it the Command object, where it gets stored until it is needed. The Invoker may • execute the Command immediately • queue the Command for execution on another thread (perhaps at a later time) • wait until the client requests for the Command to be executed Execution of a Command means calling the Command’s execute() method This results in the actions being invoked on the Receiver.

  9. Implementing “Undo” SE-2811 Dr. Mark L. Hornick

  10. UNDO: Sequence of Events The client calls the invocation method on an Invoker object and passes it the Command object, • To implement UNDO, the Invoker: • Stores each command it executes on a STACK • When UNDO is invoked, the commands are popped off the stack and un-executed Un-Execution of a Command means calling the Command’s unexecute() method • This results in the previous actions being reversed on the Receiver. • It may not be possible to reverse all actions

More Related