Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Command Pattern

3 732 vues

Publié le

Presentation given on Nov 2006 at Melbourne Patterns Group http://htxt.it/jgnK

Publié dans : Technologie, Business
  • Soyez le premier à commenter

Command Pattern

  1. 1. Command Pattern Also Known As Action, Transaction
  2. 2. Intent <ul><li>Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations. </li></ul>
  3. 3. Motivation <ul><li>To issue requests to objects without knowing anything about the operation being requested or the receiver of the request. </li></ul>
  4. 4. Example
  5. 5. Applicability <ul><li>O-O replacement for call-back function </li></ul><ul><li>Specify, queue & execute requests at different times </li></ul><ul><li>Support Undo </li></ul><ul><li>Log changes to be reapplied on a crash </li></ul><ul><li>Model system in terms of high-level transactions are build on primitive operations. </li></ul>
  6. 6. Structure
  7. 7. Collaborations
  8. 8. Consequences <ul><li>Decouples invoker from implementation of response </li></ul><ul><li>Commands are first-class objects. </li></ul><ul><li>Composite Commands </li></ul><ul><li>Easy to add new commands </li></ul>
  9. 9. Related Patterns <ul><li>A Composite can be used to implement Macro-Commands. </li></ul><ul><li>A Memento can keep state the command requires to undo its effect. </li></ul><ul><li>A command that must be copied before being placed on the history list acts as a Prototype. </li></ul>
  10. 10. “ Unit of Work” as a collection of Commands Craig Larman <ul><li>Register inserts, updates and delete </li></ul><ul><li>Sort for Referential Integrity </li></ul><ul><li>Commit </li></ul><ul><li>Uses Unit of Work Pattern from Patterns of Enterprise Application Architecture by Martin Fowler </li></ul>
  11. 11. Command Processor POSA Buschmann et al Pattern-Oriented Software Architecture <ul><li>Example from Craig Larman (Applying UML and Patterns) </li></ul><ul><ul><li>Server-side request handling </li></ul></ul><ul><ul><ul><li>Queue, log, prioritize and execute commands </li></ul></ul></ul>
  12. 12. Command Processor Intent <ul><li>Manages requests as separate objects, schedules their execution and provides additional services such as the storing of request object for later undo. </li></ul>
  13. 13. Command Processor Applicability <ul><li>Different modes of user interaction </li></ul><ul><li>External control of application via scripting language </li></ul><ul><li>Implementing crosscutting services e.g. undo, redo, macros, logging, requesting scheduling and suspension. </li></ul>
  14. 14. Command Processor Benefits <ul><li>Flexibility in the way requests are activated. </li></ul><ul><li>Flexibility in the number and functionality of requests. </li></ul><ul><li>Programming execution-related services. </li></ul><ul><li>Testability at application level. </li></ul><ul><li>Concurrency. </li></ul>
  15. 15. Command Processor Liabilities <ul><li>Efficiency loss. </li></ul><ul><li>Potential for an excessive number of command classes. </li></ul><ul><ul><li>Grouping commands </li></ul></ul><ul><ul><li>Passing the supplier object as parameter for simple objects </li></ul></ul><ul><ul><li>Macro-command objects that are combinations of simpler commands. </li></ul></ul><ul><li>Complexity in acquiring command parameters </li></ul>
  16. 16. Command Processor Variants <ul><li>Spread controller functionality. </li></ul><ul><li>Combination with Interpreter pattern. </li></ul>
  17. 17. Replace Conditional Dispatcher with Command Joshua Kerievsky <ul><li>Motivation </li></ul><ul><ul><li>Not enough runtime flexibility </li></ul></ul><ul><ul><li>A bloated body of code </li></ul></ul><ul><li>Benefits </li></ul><ul><ul><li>Execute diverse behaviour in a uniform way </li></ul></ul><ul><ul><li>Dynamically change timing and behaviour of response to requests </li></ul></ul><ul><ul><li>Trivial Implementation </li></ul></ul><ul><li>Liability </li></ul><ul><ul><li>Complicates design </li></ul></ul>