Jump to Menu

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.

Compatible edition(s): Enterprise, Professional, Standard

  • October 27, 2009
  • Views: 32,739
  • PDF

Modeling Design Pattern with Class Diagram

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

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 design pattern
  3. In the Define Design Pattern dialog box, specify the pattern name State. 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 state pattern in modeling a life.

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



創造美好 共同成長

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