Jump to Menu

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.

Compatible edition(s): Enterprise, Professional, Standard

  • October 27, 2009
  • Views: 31,108
  • PDF

Modeling Design Pattern with Class Diagram

  1. Create a new project Design Patterns.
  2. Create a class diagram Strategy.
    new class diagram
  3. Select Class from diagram toolbar. Click on the diagram to create a class. Name it as Context.
    new context class
  4. Right-click on the Context class, and select Add > Operation from the popup menu.
    add oper to context
  5. Name the operation ContextInterface().
    new context interface
  6. Move the mouse cursor over the Context class, and drag out Aggregation > Class to create an associated class Strategy.
    create strategy class
  7. Right-click on Strategy, and select Model Element Properties > Abstract to set it as abstract.
    set strategy abstract
  8. Right-click on the Strategy class, and select Add > Operation from the popup menu.
    add oper to strategy
  9. Name the operation AlgorithmInterface().
    algorithm interface added
  10. Right-click on AlgorithmInterface, and select Model Element Properties > Abstract to set it as abstract.
    set algorithm interface abstract
  11. Move the mouse cursor over the Strategy class, and drag out Generalization > Class to create subclasses ConcreteStrategy.
    create concrete strategy
  12. 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.
    realize interface
  13. 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.
    stereotype concrete strategy
  14. 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.
    set class ptn cloneable
    Up to now, the diagram should look like:
    pattern modeled

Defining 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 pattern
  3. In the Define Design Pattern dialog box, specify the pattern name Strategy. Keep the file name as is. Click OK to proceed.
    name pattern

Applying Design Pattern on Class Diagram

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

  1. Create a new project Game.
  2. Create a class diagram Domain Model.
  3. Right-click on the class diagram and select Utilities > Apply Design Pattern... from the popup menu.
    apply design pattern
  4. In the Design Pattern dialog box, select Strategy from the list of patterns.
    select strategy
  5. At the bottom pane, rename Context, Strategy and ConcreteStrategy to Game, Sprite and Warrior.
    rename classes
  6. Rename operations AlgorithmInterface to display.
    rename opers
  7. 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.
    clone
  8. Enter 2 to be the number of classes to clone. Click OK to confirm.
    clone count
  9. Rename ConcreteStrategy2 and ConcreteStrategy3 to Monster and NPC, and operations AlgorithmInterface to display.
    rename classes
  10. Click OK to apply the pattern to diagram.
  11. Tidy up the diagram. Here is the result:
    pattern modeled



创造美好 共同成长

We use cookies to offer you a better experience. By visiting our website, you agree to the use of cookies as described in our Cookie Policy.

OK