Download presentation
Presentation is loading. Please wait.
Published byGyles Tate Modified over 9 years ago
1
Computer Science 313 – Advanced Programming Topics
2
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 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 Strategy s
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 Strategy s
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 Strategy independent 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 But pattern has one way 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 Intent Encapsulate single request within an instance Client code can then pass requests as parameter Pattern treats Command as normal object Queue Command s to execute in certain order Once executed, record Command s in log
8
Command Pattern Usage Command s support undo & redo options Additional method in Command for undo support Use Command ’s state to record what it did Stack holds Command s once executed Actions then are un-done by popping Command s
9
Command Pattern Intent Split execution from performance Pattern creates one class executes Command s Command can select actor who performs it
10
Decoupling Command pattern decouples (splits) What we want to do Who does it When it is done
11
Decoupling Command pattern decouples (splits) What we want to do Who does it When it is done Wish I knew about the Command Pattern
12
For Next Class
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.