/Design Patterns /Behavioral patterns

Strategy Design Pattern

Intent

  • Define a family of algorithms, encapsulate each one,and make them interchangeable. Strategy lets the algorithmvary independently from the clients that use it.
  • Capture the abstraction in an interface, buryimplementation details in derived classes.

Problem

One of the dominant strategies of object-orienteddesign is the "open-closed principle".

Figure demonstrates how this is routinely achieved -encapsulate interface details in a base class, and buryimplementation details in derived classes. Clients canthen couple themselves to an interface, and not have toexperience the upheaval associated with change: no impactwhen the number of derived classes changes, and no impactwhen the implementation of a derived class changes.

Strategy scheme

A generic value of the software community for years has been,"maximize cohesion and minimize coupling". The object-orienteddesign approach shown in figure is all about minimizingcoupling. Since the client is coupled only to an abstraction(i.e. a useful fiction), and not a particular realization ofthat abstraction, the client could be said to be practicing"abstract coupling" . an object-oriented variant of the moregeneric exhortation "minimize coupling".

A more popular characterization of this "abstract coupling"principle is "Program to an interface, not an implementation".

Clients should prefer the "additional level of indirection"that an interface (or an abstract base class) affords.The interface captures the abstraction (i.e. the "useful fiction")the client wants to exercise, and the implementations of thatinterface are effectively hidden.

Structure

The Interface entity could represent either anabstract base class, or the method signature expectations bythe client. In the former case, the inheritance hierarchyrepresents dynamic polymorphism. In the latter case, the Interfaceentity represents template code in the client and the inheritancehierarchy represents static polymorphism.

Strategy scheme

Example

A Strategy defines a set of algorithms that can be used interchangeably.Modes of transportation to an airport is an example of a Strategy.Several options exist such as driving one's own car, taking a taxi,an airport shuttle, a city bus, or a limousine service.For some airports, subways and helicopters are also availableas a mode of transportation to the airport. Any of these modesof transportation will get a traveler to the airport, and theycan be used interchangeably. The traveler must choose the Strategybased on trade-offs between cost, convenience, and time.

Strategy example

Check list

  1. Identify an algorithm (i.e. a behavior) that the clientwould prefer to access through a "flex point".
  2. Specify the signature for that algorithm in an interface.
  3. Bury the alternative implementation details in derived classes.
  4. Clients of the algorithm couple themselves to the interface.

Rules of thumb

  • Strategy is like Template Method except in its granularity.
  • State is like Strategy except in its intent.
  • Strategy lets you change the guts of an object. Decoratorlets you change the skin.
  • State, Strategy, Bridge (and to some degree Adapter) havesimilar solution structures. They all share elements of the'handle/body' idiom. They differ in intent - that is, theysolve different problems.
  • Strategy has 2 different implementations, the first issimilar to State. The difference is in binding times (Strategyis a bind-once pattern, whereas State is more dynamic).
  • Strategy objects often make good Flyweights.

Support our free website and own the eBook!

  • 22 design patterns and 8 principles explained in depth
  • 406 well-structured, easy to read, jargon-free pages
  • 228 clear and helpful illustrations and diagrams
  • An archive with code examples in 4 languages
  • All devices supported: EPUB/MOBI/PDF formats
Learn more...

Code examples

Hey, check out our newebook on design patterns. The book covers 22 patterns and 8 design principles, all supplied with code examples and illustrations. Clear, short and fun!

Oh, and it is on saleright now.