Créer un site internet

Strategy Design Pattern

According to the Gang of four, strategy design patterns:
 
Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
In other words, strategy defines an interface common to multiple algorithms for a specific task and  enables selecting a concrete algorithm to be used at run-time. The Strategy pattern is known as a behavioural pattern
Example of this pattern is Collection.sort() which takes Comparator as argument and uses the method depending
 

Strategy Pattern Class Diagram Sample

The following is the class diagram of an example that uses a particular behavior (Fly,  Walk and Swing) on a particular shape (Circle). The choice of a specific behavior algorithm depends on the client's decision (pressed key) at runtime.
Strategydp
Figure 1:  Class diagram of program's strategy patten example
Strategy2Figure 2: Code presenting the action depends on the key pressed
 
What is the difference between Bridge and Strategy design patterns:

The UML class diagram for the Strategy pattern is the same as the diagram for the Bridge pattern. However, these two design patterns aren't the same in their intent. While the Strategy pattern is meant for behavior, the Bridge pattern is meant for structure.

The coupling between the context and the strategies is tighter than the coupling between the abstraction and the implementation in the Bridge pattern.

References :

James R. Trott; Alan Shalloway .Design Patterns Explained: A New Perspective on Object-Oriented Design, Second Edition. Addison-Wesley Professional, 2004

Add a comment