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.

October 27, 2009
Views: 41,759
PDF Download

Modeling a Design Pattern with a Class Diagram

  1. Create a new project named Design Patterns.
  2. Create a class diagram named Strategy.
    New class diagram
  3. Select Class from the diagram toolbar. Click on the diagram to create a class and name it Context.
    New context class
  4. Right-click on the Context class and select Add > Operation from the popup menu.
    Add operation to Context
  5. Name the operation `ContextInterface()`.
    New ContextInterface operation
  6. Move the mouse cursor over the Context class and drag out Aggregation > Class to create an associated class named Strategy.
    Create Strategy class
  7. Right-click on Strategy and select Model Element Properties > Abstract to set it as abstract.
    Set Strategy as abstract
  8. Right-click on the Strategy class and select Add > Operation from the popup menu.
    Add operation to Strategy
  9. Name the operation `AlgorithmInterface()`.
    AlgorithmInterface operation added
  10. Right-click on the AlgorithmInterface() operation and select Model Element Properties > Abstract to set it as abstract.
    Set AlgorithmInterface as abstract
  11. Move the mouse cursor over the Strategy class and drag out Generalization > Class to create a subclass named ConcreteStrategy.
    Create ConcreteStrategy
  12. 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.
    Realize all interfaces
  13. 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.
    Stereotype ConcreteStrategy
  14. 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.
    Set class as PTN Cloneable
    At this point, the diagram should look like this:
    Pattern modeled

Defining the Pattern

  1. Select all classes on the class diagram.
    Select all classes
  2. Right-click on the selection and select Define Design Pattern... from the popup menu.
    Define Design Pattern menu
  3. In the Define Design Pattern dialog box, specify the pattern name as Strategy. Keep the file name as is. Click OK to proceed.
    Name pattern

Applying a Design Pattern to a Class Diagram

In this section, we are going to apply the strategy pattern in modeling a video game.

  1. Create a new project named Game.
  2. Create a class diagram named Domain Model.
  3. Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.
    Apply Design Pattern menu
  4. In the Design Pattern dialog box, select Strategy from the list of patterns.
    Select Strategy pattern
  5. In the bottom pane, rename Context, Strategy, and ConcreteStrategy to Game, Sprite, and Warrior, respectively.
    Rename classes
  6. Rename the `AlgorithmInterface` operation to `display`.
    Rename operations
  7. We need two more concrete strategies for Monster and NPC. Click the + button in the ConcreteStrategy row and select Clone... from the popup menu.
    Clone
  8. Enter `2` as the number of classes to clone and click OK to confirm.
    Clone count
  9. Rename ConcreteStrategy2 and ConcreteStrategy3 to Monster and NPC, and their `AlgorithmInterface` operations to `display`.
    Rename cloned classes
  10. Click OK to apply the pattern to the diagram.
  11. Tidy up the diagram. Here is the result:
    Pattern modeled