Download
slide1 n.
Skip this Video
Loading SlideShow in 5 Seconds..
Strategy Pattern PowerPoint Presentation
Download Presentation
Strategy Pattern

Strategy Pattern

124 Vues Download Presentation
Télécharger la présentation

Strategy Pattern

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Csci 490 / Engr 596Special Topics / Special ProjectsSoftware Design and Scala ProgrammingSpring Semester 2010Lecture Notes

  2. Strategy Pattern This is a set of slides to accompany chapter 8 of Mark Grand’s book Patterns in Java : a catalog of reusable design patterns illustrated with UML (John Wiley & Sons, 1998) Created: 19 August 2004 Revised: 20 April 2010

  3. Context • Any one of a family of algorithms may do a task • Wish to make then dynamically interchangeable • Invoke only operations on the base class • Delegate creation of the actual subclass object to a special class – factory 1

  4. General Approach • Common interface to family • abstract class/interface • Encapsulate algorithm into implementing (sub)class • Delegate task by using interface 2

  5. Hoilday getHolidays(:Date):String[] Uses 1..* CalendarDisplay Uses 1 0..1 … USHoliday CanadaHoliday CompostiteHoliday Example • CalendarDisplay uses a Holiday instance to determine what holidays fall on each date • Actually use instance of an appropriate subclass • Composite Holiday further delegates to several other subclasses 3

  6. A class in this role provides common way to access operation encapsulated by its subclasses. AbstractStrategy Operation() Uses Client 1 0..1 A class in thisrole delegates an operation to an abstract class or interface. … ConcreteStrategy2 ConcreteStrategy1 Classes in thisrole implement alternative implementations of the operation that the client class delegate. Solution 4

  7. Consequences • Client behavior dynamically determined object by object • Client class simplified • No need to select/implement the alternative behavior • Need way to configure 5

  8. Example CodeClient class CalendarDisplay { private Holiday holiday; private static final String [] noHoliday = new String[0]; … // Private class used to cache information about dates private class DateCache { private Date date; private String[] holidayStrins; DateCache (Date dt) { date = dt; … if (holiday == null) holidayStrings = noHoliday; else HolidayStrings = holiday.getHolidays(date); … } // constructor(Date) }//class DateCache … } //class CalendatDisplay 6

  9. Example CodeAbstractStrategy public abstract class Holiday { protected final static String [] noHoliday = new String[0]; // Return array of strings describing holidays falling on a date // If no holidays fall on the given date, returns a zero length array abstract public String[] getHolidays(Date dt); } // class Holiday 7

  10. Example CodeConcreteStrategy public class USHoliday extends Holiday { … public String [ ] getHolidays(Date dt) { String [ ] holidays = noHoliday; … return holidays; } //getHolidays(Date) } //class USHoliday 8

  11. Acknowledgement This work was supported by a grant from Acxiom Corporation titled “The Acxiom Laboratory for Software Architecture and Component Engineering (ALSACE).” 9