Strategy Pattern Tutorial
This tutorial is aimed to guide the definition and application of Gang of Four (GoF) strategy design pattern. By reading this tutorial, you will know how to develop a model for the strategy pattern, and how to apply it in practice.
Modeling a Design Pattern with a Class Diagram
- Create a new project named Design Patterns.
- Create a class diagram named Strategy.

- Select Class from the diagram toolbar. Click on the diagram to create a class and name it 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 named 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 the AlgorithmInterface() operation 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 a subclass named ConcreteStrategy.

- We need to make the concrete strategy 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 `ConcreteStrategy` class as PTN Cloneable. Right-click 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 the ConcreteStrategy class. Click OK to confirm.
At this point, the diagram should look like this:
Defining the 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 as Strategy. Keep the file name as is. Click OK to proceed.

Applying a Design Pattern to a Class Diagram
In this section, we are going to apply the strategy pattern in modeling a video game.
- Create a new project named Game.
- Create a class diagram named 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.

- In the bottom pane, rename Context, Strategy, and ConcreteStrategy to Game, Sprite, and Warrior, respectively.

- Rename the `AlgorithmInterface` operation to `display`.

- We need two more concrete strategies for Monster and NPC. Click the + button in the ConcreteStrategy row and select Clone... from the popup menu.

- Enter `2` as the number of classes to clone and click OK to confirm.

- Rename ConcreteStrategy2 and ConcreteStrategy3 to Monster and NPC, and their `AlgorithmInterface` operations to `display`.

- Click OK to apply the pattern to the diagram.
- Tidy up the diagram. Here is the result:
