System Design  «Prev  Next»
Lesson 5 State design pattern
ObjectiveExplain how to map the statechart to the state design pattern.

State Design Pattern

How is the State Design Pattern used in system design?
The State Design Pattern is a behavioral pattern used in system design to manage the behavior of an object based on its internal state. It allows an object to change its behavior dynamically as its state changes, improving code flexibility and maintainability.
Here's how it works:
  • Core Concept: The pattern revolves around creating separate classes or structures to represent each possible state of an object. These state classes encapsulate the behavior specific to that state.
  • Context Object: A central "context object" holds a reference to the current state object. This context object interacts with the user or other parts of the system.
  • State Transitions: When an event occurs that triggers a state change, the context object changes its reference to the appropriate state class. This effectively changes the object's behavior.
Benefits of using the State Design Pattern:
  • Improved Code Readability and Maintainability: By separating behavior into distinct state classes, the code becomes more organized and easier to understand. Modifying behavior for a particular state only requires changes within the dedicated state class.
  • Reduced Code Duplication: If the same behavior exists across multiple states with minor variations, the pattern avoids code duplication by placing the common logic in a base state class and allowing specific states to override it for their variations.
  • Flexible State Management: The pattern allows for easy addition of new states in the future without major code restructuring. Simply create a new state class and update the context object to handle transitions to the new state.
Real-world example: Consider a vending machine. It can be in different states like "Idle" (waiting for user input), "CoinInserted" (accepting coins), "ProductSelected" (product chosen), and "DispensingProduct" (delivering the selected item).
  • Each state would be a separate class with methods specific to that state's behavior.
  • The vending machine itself would be the context object, holding a reference to the current state.
  • Events like inserting coins, selecting a product, or dispensing the product would trigger transitions between states, dynamically changing the vending machine's behavior based on its current state.

The State Design Pattern is a valuable tool for managing complex object behavior in system design, especially when the behavior is highly dependent on the object's internal state.** It promotes code organization, maintainability, and flexibility for handling various state transitions within your system.

Managing state-specific behavior

When an object exhibits a lot of state-specific behavior the code can become large, complex, and difficult to follow. For each behavior that the object can manifest, the implementation may be different for each state of the object. For example, a relatively simple object with six states and six behaviors would require 36 blocks of implementation code, one block of code for each behavior for each state.
View the image below to see an example of this.
State Code
State Code

State Design Pattern

State Pattern One technique for coping with the complexity of state-specific behavior is the state design pattern. This pattern uses the concept of delegation to separate the implementation of a behavior from the object that is responsible for the behavior. For example, most of us are responsible for filing taxes. However, for some of us, the process is extremely complex and requires assistance from an expert. We retain ultimate responsibility for filing the taxes, but we delegate the actual process to a specialist who does it for us. When the tax specialist is finished, he/she returns the results to us. View the diagram below to see the key components of the state design pattern.
State Design Pattern
State Design Pattern

When an object's behaviors differ depending on the object's state, you can define new objects that represent the object's state. Each unique state object type has its own implementation for each behavior. When the object needs a behavior it "delegates" the behavior to the appropriate state object. Once the behavior has been completed, the state object returns control to the original object. Let us look at a Java example:
void SubmitClaim(Claim) { 
  state.SubmitClaim(Claim) 
}

Note how the operation simply invokes another operation of the same name on the object referred to by the state attribute. The interface does not really even have an implementation of its own.

Map the state diagram to the state pattern

The statechart diagram provides an explicit description of the states of an object. To create the state pattern in your class diagram click the View Image button and follow the steps.
State Design Pattern
The state pattern is not appropriate for every application of state-specific behavior. However, when you are faced with increasing numbers of states and a variety of behaviors, the pattern can pay huge dividends in ease of maintenance and understandability.