The State Design Pattern is a behavioural design pattern that allows an object to alter its behaviour when its internal state changes. This pattern is particularly useful when an object transitions through different states, and its behaviour needs to change accordingly. In this blog post, we’ll explore the State Design Pattern, its components, and how it can be effectively applied in software development.
Table of Contents
What?
The state design pattern is a behavioural design pattern that allows an object to change its behaviour when its internal state changes

Code
Let’s implement an Order Processing System where an order can be in different states:
- New → Processing → Shipped → Delivered
Each state will have its own behavior.
from abc import ABC, abstractmethod
# State Interface
class OrderState(ABC):
@abstractmethod
def next_state(self, order):
pass
@abstractmethod
def get_status(self):
pass
# Concrete States
class NewOrder(OrderState):
def next_state(self, order):
order.state = Processing()
def get_status(self):
return "Order is placed and waiting for processing."
class Processing(OrderState):
def next_state(self, order):
order.state = Shipped()
def get_status(self):
return "Order is being processed."
class Shipped(OrderState):
def next_state(self, order):
order.state = Delivered()
def get_status(self):
return "Order is shipped."
class Delivered(OrderState):
def next_state(self, order):
print("Order is already delivered. No further state changes.")
def get_status(self):
return "Order is delivered."
# Context Class
class Order:
def __init__(self):
self.state = NewOrder() # Initial state
def proceed(self):
self.state.next_state(self)
def status(self):
return self.state.get_status()
# Client Code
if __name__ == "__main__":
order = Order()
print(order.status()) # Order is placed
order.proceed()
print(order.status()) # Processing
order.proceed()
print(order.status()) # Shipped
order.proceed()
print(order.status()) # Delivered
order.proceed() # No further state change
PythonOutput
Order is placed and waiting for processing.
Order is being processed.
Order is shipped.
Order is delivered.
Order is already delivered. No further state changes.
PythonUse Case of the State Design Pattern
The State Design Pattern finds applications in various real-world scenarios where an object needs to alter its behaviour based on internal state changes. Here are a few real-world applications of the State Design Pattern:
- Document Editing Software: Consider a document editing application where the behaviour of the editing tools (such as drawing, text editing, or formatting) depends on the current state of the application (e.g., edit mode, draw mode, or formatting mode). The State Design Pattern could be used to manage the transitions between these different modes efficiently.
- Workflow Management Systems: In workflow management systems, different tasks or processes may have various states. The State Design Pattern can be applied to model the behaviour of a task or process based on its current state, allowing for cleaner and more modular code.
- Vending Machines: Vending machines go through different states based on user interactions (e.g., idle, selecting a product, dispensing, etc.). The State Design Pattern can be used to represent and manage these states, ensuring that the vending machine responds appropriately to user actions.
- Game Development: In video game development, characters or game entities often have different states (e.g., idle, walking, running, attacking). The State Design Pattern can be employed to model and control the behaviour of these entities based on their current state, providing a flexible and maintainable way to manage game logic.
- Network Connection Handling: Network connections can have various states (e.g., connected, disconnected, waiting for response). The State Design Pattern can be utilized to model the behaviour of a network connection based on its current state, making it easier to manage connection-related actions and transitions.
In each of these examples, the State Design Pattern helps to organize and encapsulate the behaviour associated with different states, making the code more modular, readable, and easier to maintain. It allows for a more flexible and extensible design by isolating the logic for each state into separate classes.
Conclusion
The State Design Pattern is a powerful tool for managing the behaviour of objects that undergo state transitions. By encapsulating the behaviour in state-specific classes, the pattern promotes a clean and modular design, making it easier to understand, extend, and maintain the code. Consider using the State Design Pattern when dealing with objects that exhibit different behaviours based on their internal state.
Resources
For further exploration, make sure to check out these helpful resources:
1 thought on “Understanding the State Design Pattern”