Top Banner
Command Pattern Also Known As Action, Transaction
17

Command Pattern Geoff Burns 2006 Nov

May 06, 2015

Download

Technology

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Command Pattern Geoff Burns 2006 Nov

Command Pattern

Also Known As

Action, Transaction

Page 2: Command Pattern Geoff Burns 2006 Nov

Intent

• Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.

Page 3: Command Pattern Geoff Burns 2006 Nov

Motivation

• To issue requests to objects without knowing anything about the operation being requested or the receiver of the request.

Page 4: Command Pattern Geoff Burns 2006 Nov

Example

Page 5: Command Pattern Geoff Burns 2006 Nov

Applicability

• O-O replacement for call-back function

• Specify, queue & execute requests at different times

• Support Undo

• Log changes to be reapplied on a crash

• Model system in terms of high-level transactions are build on primitive operations.

Page 6: Command Pattern Geoff Burns 2006 Nov

Structure

Page 7: Command Pattern Geoff Burns 2006 Nov

Collaborations

Page 8: Command Pattern Geoff Burns 2006 Nov

Consequences

• Decouples invoker from implementation of response

• Commands are first-class objects.

• Composite Commands

• Easy to add new commands

Page 9: Command Pattern Geoff Burns 2006 Nov

Related Patterns

• A Composite can be used to implement Macro-Commands.

• A Memento 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.

Page 10: Command Pattern Geoff Burns 2006 Nov

“Unit of Work” as a collection of Commands Craig Larman

• Register inserts, updates and delete

• Sort for Referential Integrity

• Commit

• Uses Unit of Work Pattern from Patterns of Enterprise Application Architecture by Martin Fowler

Page 11: Command Pattern Geoff Burns 2006 Nov

Command Processor

POSA Buschmann et al Pattern-Oriented Software Architecture

• Example from Craig Larman (Applying UML and Patterns)

– Server-side request handling

• Queue, log, prioritize and execute commands

Page 12: Command Pattern Geoff Burns 2006 Nov

Command Processor Intent

• Manages requests as separate objects, schedules their execution and provides additional services such as the storing of request object for later undo.

Page 13: Command Pattern Geoff Burns 2006 Nov

Command Processor Applicability

• Different modes of user interaction

• External control of application via scripting language

• Implementing crosscutting services e.g. undo, redo, macros, logging, requesting scheduling and suspension.

Page 14: Command Pattern Geoff Burns 2006 Nov

Command Processor Benefits

• Flexibility in the way requests are activated.

• Flexibility in the number and functionality of requests.

• Programming execution-related services.

• Testability at application level.

• Concurrency.

Page 15: Command Pattern Geoff Burns 2006 Nov

Command Processor Liabilities

• Efficiency loss.• Potential for an excessive number of command

classes.– Grouping commands– Passing the supplier object as parameter for simple

objects– Macro-command objects that are combinations of

simpler commands.

• Complexity in acquiring command parameters

Page 16: Command Pattern Geoff Burns 2006 Nov

Command Processor Variants

• Spread controller functionality.

• Combination with Interpreter pattern.

Page 17: Command Pattern Geoff Burns 2006 Nov

Replace Conditional Dispatcher with CommandJoshua Kerievsky

• Motivation– Not enough runtime flexibility– A bloated body of code

• Benefits– Execute diverse behaviour in a uniform way– Dynamically change timing and behaviour of

response to requests– Trivial Implementation

• Liability– Complicates design