1 / 19

Strategy: A Behavioral Design Pattern

Strategy: A Behavioral Design Pattern. By Anne Ryan. What is a Design Pattern?. A design pattern deals with interactions between individual software components They are recurring solutions to common problems of design!. Types of Design Patterns. Creational Structural Behavorial

aren
Download Presentation

Strategy: A Behavioral Design 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. Strategy:A Behavioral Design Pattern By Anne Ryan

  2. What is a Design Pattern? A design pattern deals with interactions between individual software components They are recurring solutions to common problems of design!

  3. Types of Design Patterns • Creational • Structural • Behavorial • Strategy is a behavioral design pattern

  4. Strategy In a Strategy design pattern, you will: • Define a family of algorithms • Encapsulate each one • Make them interchangeable

  5. You should use Strategy when: • You have code with a lot of algorithms • You want to use these algorithms at different times • You have algorithm(s) that use data the client should not know about

  6. Context contextInterface() ConcreteStrategyC ConcreteStrategyB ConcreteStrategyA algorithmInterface() algorithmInterface() algorithmInterface() Strategy Class Diagram Strategy algorithmInterface()

  7. Strategy vs. Subclassing • Strategy can be used in place of subclassing • Strategy is more dynamic • Multiple strategies can be mixed in any combination where subclassing would be difficult

  8. Class functionX() SubClass3 SubClass2 SubClass1 functionX() functionX() functionX() Subclassing

  9. Class functionX()functionY() SubClass3 SubClass2 SubClass1 functionX() functionX() functionX() Add a function Add functionY()

  10. Class functionX()functionY() SubClass3 SubClass2 SubClass1 functionrX()functionY() functionX()functionY() functionX()functionY() SubClass2.2 SubClass2.1 behaviorY() behaviorY() What happens? Need SIX classes to handle both functions!!!

  11. StrategyX functionX() ... Class functionX()functionY() StrategyY functionY() ... Strategy makes this easy!

  12. Benefits of Strategy • Eliminates conditional statements • Can be more efficient than case statements • Choice of implementation • Client can choose among different implementations with different space and time trade-offs

  13. Benefits of Strategy • Families of related algorithms • Alternative to subclassing • This lets you vary the algorithm dynamically, which makes it easier to change and extend • You also avoid complex inheritance structures

  14. Drawbacks of Strategy • Clients must be aware of different strategies • Clients must know how strategies differ so it can select the appropriate one • Communication overhead between strategy and context • Sometimes the context will create and initialize parameters that are never used

  15. Drawbacks of Strategy • Increased number of objects • if the algorithm differences are simple, the extra classes add extra complexity

  16. Context contextInterface() ConcreteStrategyC ConcreteStrategyB ConcreteStrategyA algorithmInterface() algorithmInterface() algorithmInterface() Implementation Issues Strategy algorithmInterface()

  17. Implementation Issues • Concrete Strategy needs efficient access to data • Should you use a template? • Should you make strategy objects optional?

  18. Thank you to Provider of the PowerPoint graphs: http://vik.ktu.lt/moduliai/

  19. What have we learned today about strategy?

More Related