Friday, October 23, 2015

Design pattern - Strategy pattern

Description:
Define a family of algorithms, encapsulate each one and make it interchangeable. Strategy lets the algorithm vary independently from the client use it.

In short - "Encapsulates an algorithm inside a class"

Problem:
Sometimes new behavior or algorithm or action on related objects are changed or new implementation needs to be provided. Using interface leaves each concrete class to have it's own implementation, no way to reuse/leverage exists implementation. Using class inheritance leaves in situation where all sub classes are forces to provide implementation of the behavior even not appropriate to that sub class.
Strategy pattern encapsulates set of behaviors/algorithms/actions in a separate hierarchy allowing client to use algorithm without knowing implementation and run time change of algorithm. Other hand behavioral implementation changes can be done without affecting client.

How to use Strategy pattern:
1. Concepts involved in Strategy pattern, 'context', 'abstract behavior', 'concrete behavior' 

2. Create an interface 'behavior' with abstract method (like execute/do). 

3. Create derived concrete class(es) from 'behavior' base class which will provide the implementation of the abstract method(s) of interface. Each concrete class will have alternative implementation detail

4. Need to create separate behavior interface and concrete class creation for each set of behaviors

5. Client will create an instance of concrete behavior and store it in abstract reference. Client class will have a entry point (a method) which will make the abstract method call on abstract reference

  • Most of the times, client side has a hierarchy where concrete classes derived from an abstract class, the abstract class holds the reference of strategy provided by concrete class and has a method to make a call to abstract strategy reference without knowing the actual concrete class.

Class diagram:























Examples:
1. Duck pond simulation game from Head First Design Pattern. The large variety of duck species can swim but in need of adding behaviors like fly, make quacking sounds on some specific kind of ducks. Fly behavior can be externalized through strategy and ducks would just use composition of fly behavior.

2. 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 available as a mode of transportation to the airport. Any of these modes of transportation will get a traveler to the airport, and they can be used interchangeably. The traveler must chose the Strategy based on tradeoffs between cost, convenience, and time. (Resources-2)

3. A car objects break behavior can be considered as strategy where regular break or ABS break can be used based on need.

Benefits:
1. Decouples the algorithm/behavior from client who would use it
2. Allows client to pick any implementation of algorithm/behavior
3. Allows algorithms/behaviors to interchange without affecting client
Notes:
1. Strategy pattern uses composition between objects rather than inheritance 
2. Follows 'Open/Closed Principle' of OO Design Principle
3. Abstract coupling - client is coupled only to an abstraction and not a particular realization of that abstraction
4. Ideally client should have a mean (mostly using setter method) to change strategy


Things to check back:
1. Coexistence with other patterns


Resources:
1. Head First Design Pattern
2. https://sourcemaking.com/design_patterns
3. https://en.wikipedia.org/wiki/strategy_pattern

No comments: