State Pattern Tutorial

This tutorial is aimed to guide the definition and application of Gang of Four (GoF) state design pattern. By reading this tutorial, you will know how to develop a model for the state pattern, and how to apply it in practice.

October 27, 2009
Views: 38,960
PDF Download

Modeling a Design Pattern with a Class Diagram

  1. Create a new project named Design Patterns.
  2. Create a class diagram named State.
    New diagram
  3. Select Class from the diagram toolbar. Click on the diagram to create a class and name it Context.
    Create 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 `Request()`.
    Name operation Request
  6. Move the mouse cursor over the Context class and drag out Aggregation > Class to create an associated class named State.
    Create State class
  7. Right-click on State and select Model Element Properties > Abstract to set it as abstract.
    Set State as abstract
  8. Right-click on the State class and select Add > Operation from the popup menu.
    Add operation to State
  9. Name the operation `Handle()`.
    Name operation Handle
  10. Right-click on the Handle() operation and select Model Element Properties > Abstract to set it as abstract.
    Set Handle as abstract
  11. Move the mouse cursor over the State class and drag out Generalization > Class to create a subclass named ConcreteState.
    Create ConcreteState subclass
  12. We need to make the concrete state inherit operations from the state class. Right-click on ConcreteState and select Related Elements > Realize all Interfaces from the popup menu.
    Realize all interfaces for ConcreteState
  13. In practice, there may be multiple concrete states. To represent this, stereotype the `ConcreteState` class as PTN Cloneable. Right-click on ConcreteState and select Stereotypes > Stereotypes... from the popup menu.
    Stereotype ConcreteState
  14. In the Stereotypes tab of the Class Specification dialog box, select PTN Cloneable and click > to assign it to the ConcreteState class. Click OK to confirm.
    Select PTN Cloneable stereotype

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 State. 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 state pattern in modeling a 'Life' concept.

  1. Create a new project named Life.
  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 State from the list of patterns.
    Select State pattern
  5. In the bottom pane, rename Context, State, and ConcreteState to Life, LifeState, and IdleState, respectively.
    Rename classes
  6. We need two more concrete states for 'up' and 'down'. Click the + button in the `ConcreteState` row and select Clone... from the popup menu.
    Clone state
  7. Enter `2` as the number of classes to clone and click OK to confirm.
    Clone count
  8. Rename ConcreteState2 and ConcreteState3 to UpState and DownState.
    Rename cloned classes
  9. Click OK to apply the pattern to the diagram.
  10. Tidy up the diagram. Here is the result:
    Resulting diagram