This tutorial is designed to provide guidance on the definition and application of the Gang of Four (GoF) state design pattern. By following this tutorial, you will learn how to create a UML class diagram for the state pattern and save it as a design pattern file that can be reused in the future.

What is State Design Pattern?

The State Design Pattern is a behavioral design pattern that allows an object to change its behavior when its internal state changes. This pattern is used when an object’s behavior depends on its state and it must change its behavior at runtime depending on that state. It provides a way to encapsulate the behavior of an object into separate classes, representing different states, and allows the object to change its behavior by changing its state object.

In the State pattern, the class representing the object with the state is called the Context, and the different states of the object are represented by separate classes implementing a common State interface. The Context object contains a reference to the current State object, and delegates all requests to it. When the Context object needs to change its behavior, it simply changes the reference to the current State object to a new one representing a different state.

The State pattern allows for a clean separation of concerns between the behavior of the object and its state, making it easier to maintain and extend the code. It also promotes loose coupling between the Context and State objects, as they can be easily interchanged without affecting the rest of the system.

Overall, the State Design Pattern is a powerful tool for managing complex behaviors in software applications, especially those that involve objects with multiple possible states.

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

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 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 ContextState and ConcreteState to LifeLifeState 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

Resources

  1. Design Patterns.vpp
  2. State.pat

Related Links

Leave a Comment

Your email address will not be published.