Strategy Pattern Tutorial
This tutorial aims to guide you in understanding and applying the Gang of Four (GoF) Strategy design pattern. Through this tutorial, you will learn how to create a UML class diagram for the Strategy pattern and save it as a design pattern file that can be reused in the future.
What is Strategy Design Pattern?
The Strategy Design Pattern is a behavioral design pattern that allows you to encapsulate a family of algorithms and make them interchangeable at runtime. It defines a set of algorithms that can be used to perform a specific task, and allows the client to choose which algorithm to use without depending on the concrete implementation of the algorithm.
In this pattern, a context class is created that contains a reference to a strategy interface. The strategy interface defines the contract for the interchangeable algorithms, while concrete strategy classes implement the algorithm according to the defined contract.
This pattern provides an alternative to implementing conditional statements in code by allowing the client to select a strategy to use at runtime. It promotes open-closed principle by allowing the addition of new strategies without modifying the existing code.
Some real-world examples of the Strategy Design Pattern include sorting algorithms, encryption algorithms, and compression algorithms.
Modeling Design Pattern with Class Diagram
- Create a new project Design Patterns.
- Create a class diagram Strategy.
- Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Context.
- Right-click on the Context class, and select Add > Operation from the popup menu.
- Name the operation ContextInterface().
- Move the mouse cursor over the Context class, and drag out Aggregation > Class to create an associated class Strategy.
- Right-click on Strategy, and select Model Element Properties > Abstract to set it as abstract.
- Right-click on the Strategy class, and select Add > Operation from the popup menu.
- Name the operation AlgorithmInterface().
- Right-click on AlgorithmInterface, and select Model Element Properties > Abstract to set it as abstract.
- Move the mouse cursor over the Strategy class, and drag out Generalization > Class to create subclasses ConcreteStrategy.
- We need to make the concrete strategies inherit operations from the strategy class. Right-click on ConcreteStrategy and select Related Elements > Realize all Interfaces from the popup menu.
- In practice, there may be multiple concrete strategies. To represent this, stereotype the class ConcreteStrategy as PTN Cloneable. Right right on ConcreteStrategy and select Stereotypes > Stereotypes… from the popup menu.
- In the Stereotypes tab of the Class Specification dialog box, select PTN Cloneable and click > to assign it to ConcreteStrategy class. Click OK to confirm.
Up to now, the diagram should look like:
Saving the Defining Pattern
- Select all classes on the class diagram.
- Right-click on the selection and select Define Design Pattern… from the popup menu.
- In the Define Design Pattern dialog box, specify the pattern name Strategy. Keep the file name as is. Click OK to proceed.
Applying Design Pattern on Class Diagram
In this section, we are going to apply the strategy pattern in modeling a video game.
- Create a new project Game.
- Create a class diagram Domain Model.
- Right-click on the class diagram and select Utilities > Apply Design Pattern… from the popup menu.
- In the Design Pattern dialog box, select Strategy from the list of patterns.
- At the bottom pane, rename Context, Strategy and ConcreteStrategy to Game, Sprite and Warrior.
- Rename operations AlgorithmInterface to display.
- We need 2 more concrete strategy for Monster and NPC. Click on the + button at the ConcreteStrategy row and select Clone… from the popup menu.
- Enter 2 to be the number of classes to clone. Click OK to confirm.
- Rename ConcreteStrategy2 and ConcreteStrategy3 to Monster and NPC, and operations AlgorithmInterface to display.
- Click OK to apply the pattern to diagram.
- Tidy up the diagram. Here is the result: