First we should layout some ground work for discussion.
I will start with formal definition and then examples of scenarios where we can use Strategy Pattern.
To begin with here is the formal definition:
"
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it."
Some
Examples of using Strategy Pattern (I collected them from top 10 google results for Strategy Pattern):
1. Family of algorithms: SortingStrategy interface (represents family of Sorting algorithms to sort a list)
Encapsulates each one: QuickSort, MergeSort implementations of SortingStrategy.
Makes them interchangable: SortedList class having a property of type SortingStrategy.
(source:
http://www.dofactory.com/Patterns/PatternStrategy.aspx )
2. Family of algorithms: TransportationMode interface (represents family of transportation modes available from airport)
Encapsulates each one: PersonalCarMode, TaxiMode, CityBusMode etc. implementations of TransportationMode
Makes them interchangable: property of type TransportationMode in class TransportationToAirport
(source:
http://sourcemaking.com/design_patterns/strategy )
3. Family of algorithms: RobotBehaviour interface (represents family of robot behaviours)
Encapsulates each one: AggressiveBehaviour, DefensiveBehaviour, NormalBehaviour etc. implementations of RobotBehaviour
Makes them interchangable: property of type RobotBehaviour in class Robot
(source:
http://www.oodesign.com/strategy-pattern.html )
4. Family of algorithms: Border interface (defines family of borders for a Swing component)
Encapsulates each one: BevelBorder, CompoundBorder, EmptyBorder, EtchedBorder, LineBorder, TitledBorder etc. implementations of Border
Makes them interchangable: property of type Border in class JComponent
(source:
http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-designpatterns.html?page=2 )
5. Family of algorithms: ShootingBehaviour interface (defines family of shooting behaviours for a shooting game
unit)
Encapsulates each one: GunShoot, ThrowKnives, CantShoot etc. implementations of ShootingBehaviour
Makes them interchangable: property of type ShootingBehaviour in class GameUnit
(source:
http://www.cumps.be/nl/blog/read/design-patterns-strategy-pattern )
As far as my understanding Strategy Pattern defines a level of abstraction between a concrete Strategy(implementation) and users of the Strategy. Thus enabling users to afford loose coupling with implementation which results in replacable or exchangable implementations.
Further Reading:
Definition of some terms (source:
http://www.oodesign.com/strategy-pattern.html ):
Strategy - defines an interface common to all supported algorithms. Context uses this interface to call the algorithm defined by a ConcreteStrategy.
ConcreteStrategy - each concrete strategy implements an algorithm.
Context -
-contains a reference to a strategy object.
-may define an interface that lets strategy accessing its data.
The Context objects contains a reference to the ConcreteStrategy that should be used. When an operation is required then the algorithm is run from the strategy object. The Context is not aware of the strategy implementation. If necessary, addition objects can be defined to pass data from context object to strategy.
also see :
http://sourcemaking.com/design_patterns/strategy