1 / 15

Lecture 23: Command Pattern

Computer Science 313 – Advanced Programming Topics. Lecture 23: Command Pattern. Strategy Pattern. Define interface for family of algorithms Encapsulate algorithm in class of its own Algorithm instance held in field of Context class Delegate responsibility for action to this object

tracey
Download Presentation

Lecture 23: 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. Computer Science 313 – Advanced Programming Topics Lecture 23:Command Pattern

  2. Strategy Pattern • Defineinterface for family of algorithms • Encapsulate algorithm in class of its own • Algorithm instance held in field of Context class • Delegate responsibility for action to this object • Set field to change algorithm dynamically

  3. Limits To Strategy • Several limitations to strategy pattern • Details of Strategy must be known by Context • Context must execute actual Strategy • Strategy should not maintain state • Cannot determine past Strategys

  4. Limits To Strategy • Several limitations to strategy pattern • Details of Strategy must be known by Context • Context must execute actual Strategy • Strategy should not maintain state • Cannot determine past Strategys

  5. Why Is This A Problem? • Unable to log steps taken during program • But a Strategy is not a tangible object • Cannot undo a Strategy once executed • To enable “undo” would need log of actions • But Strategy lacks state to save actual results • Strategyindependent of Context • Cannot use external objects when executed • Specific context cannot host a Strategy

  6. Command Pattern • Encapsulates single action as an object • Each instance can be used one or more times • Define range of actions in abstract Command • Pattern defines onemethod to execute Command • Adjust the state of a Command using fields • Modify how it executes and what it does • Use to specify which instance Command uses • More than parameters to control exposed method

  7. Command Pattern • Encapsulates single action as an object • Each instance can be used one or more times • Define range of actions in abstract Command • Pattern defines onemethod to execute Command • Adjust the state of a Command using fields • Modify how it executes and what it does • Use to specify which instance Command uses • More than parameters to control exposed method

  8. Command Pattern Intent • Encapsulate single request within an instance • Client code can then pass requests as parameter • Pattern treats Command as normal object • Queue Commands to execute in certain order • Once executed, record Commands in log

  9. Command Pattern Usage • Commands can support undo & redo options • Additional method in Commandfor undo support • Record prior state using Command’s fields • Once executed, Stackholds Commands • Pop Command & execute undo() method in turn

  10. Command Pattern Intent • Split creation, execution, & performance • Command can select actor who performs it • Pattern creates one class executes Commands • Client class creating Command object to start

  11. Command Pattern Decoupling What we want to do Who does it When it is done

  12. Command Pattern Decoupling What we want to do Who does it When it is done We wish we knew about the Command Pattern

  13. Command Pattern Decoupling What we want to do Who does it When it is done We wish we knew about the Command Pattern We wish we knew about the Command Pattern

  14. Command Pattern Decoupling What we want to do Who does it When it is done We wish we knew about the Command Pattern We wish we knew about the Command Pattern We wish we knew about the Command Pattern

  15. For Next Class • Lab #5 on Angel & can rework on Friday • Hopefully, should reuse all your existing code • Retrofit to use pattern by factoring existing code • In the book, read pages 203 - 214 • Start implementing the Command Pattern • What classes would we need? • Who executes the command? • And what the heck is a NOP (Null Command)?

More Related