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

  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

Saving the 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 ContextStrategy and ConcreteStrategy to GameSprite 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

Resources

  1. Design Patterns.vpp
  2. Strategy.pat

Related Links

Leave a Comment

Your email address will not be published.