1 / 11

In normal words…

Strategy Define a family of algorithms, encapsulating each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Not to be confused with… Strategery

deepak
Download Presentation

In normal words…

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. StrategyDefine a family of algorithms, encapsulating each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it. Not to be confused with… Strategery Define a family of algorithmics, encapsulacating each one, and make them interjectionable. Strategery lets the algorithmic vary interdepartmentally from clients that use it. Design Patterns (Gamma, Helm, Johnson, Vlissides)

  2. In normal words… If you have an object that can do something in many different ways, rather than putting each way into a method in the object and getting confused, put each way of performing the behavior in its own class.

  3. Generic Strategy Pattern Declares interface common to all supported algorithms. Used by Context Configured with a ConcreteStrategy object. Maintains a reference to a Strategy object. Implements algorithm using Strategy interface Modified from Design Patterns (Gamma, Helm, Johnson, Vlissides)

  4. PowerPoint Strategy Pattern (Example) Modified from Design Patterns (Gamma, Helm, Johnson, Vlissides)

  5. Instead of this override PresentPowerpoint() override PresentPowerpoint() override PresentPowerpoint()

  6. You have this PerfectPowerpoint PresentPerfectPowerpoint() AlrightPowerpoint PresentAlrightPowerpoint() LamePowerpoint PresentLamePowerpoint()

  7. using System; using System.Collections.Generic; using System.Text; namespace StrategyExample { classProgram { staticvoid Main() { Presenter p = newPresenter(); p.SetPresentStrategy(newPerfectPowerPointPresentation()); p.Present(); p.SetPresentStrategy(newAlrightPowerPointPresentation()); p.Present(); p.SetPresentStrategy(newLamePowerPointPresentation()); p.Present(); Console.ReadKey(); } }

  8. abstractclassPresentStrategy { publicabstractvoid Present(); } classPerfectPowerPointPresentation : PresentStrategy { publicoverridevoid Present() { Console.WriteLine("Perfect: The presentation presented was incredible!!!"); } } classAlrightPowerPointPresentation : PresentStrategy { publicoverridevoid Present() { Console.WriteLine("Alright: The presentation presented was just alright"); } } classLamePowerPointPresentation : PresentStrategy { publicoverridevoid Present() { Console.WriteLine("Lame: The presentation presented was the one you’re watching now."); } }

  9. classPresenter { privatePresentStrategy _presentstrategy; publicvoidSetPresentStrategy(PresentStrategypresentstrategy) { this._presentstrategy = presentstrategy; } publicvoid Present() { _presentstrategy.Present(); Console.WriteLine(); } } }

  10. Results from Example

  11. When do I use this? • When many related classes differ only in their behavior. • You need different variants of an algorithm. • An algorithm uses data that clients shouldn’t know about. • In place of a conditional statement Modified from Design Patterns (Gamma, Helm, Johnson, Vlissides)

More Related