Behavioral Design Pattern - State
Design Pattern
10 Articles
In this article, let's learn about State Design Pattern
in .NET.
Table of Contents
Introduction
The State
Pattern is a behavioral
design pattern that lets an
object
alter its behavior when its state changes. Instead of using a large set of
if-else
or switch
statements,
state-specific
behavior is delegated to separate state classes
.
For example, consider a bank account
that can be in a regular
or
overdrawn
state. Each state has its own rules—when in an overdrawn state
,
withdrawals may be restricted. Adding more states, like a gold
state for high balances, can make the logic
even more complex. Using the state pattern helps to manage this complexity by organizing the behavior in
individual classes
.
Structure
The State
pattern involves several key components:
Context
: The mainclass
that maintains the current state (e.g.,BankAccount
).State
: Anabstract base class
orinterface
that defines behaviors for different states (e.g.,BankAccountState
).Concrete States
: Implementations of the state that handlespecific state behavior
(e.g.,RegularState
,OverdrawnState
).
Code Sample - State Pattern Code Sample
Demo - State Pattern Demo
Let's try State
Demo, Click on the Deposit
button and Withdraw
button to see the state pattern in action.
The results will be displayed at the bottom.
Code Sample - State Pattern Demo
State | Balance |
---|
In the above code, see how state changes automatically based on the balance. When the balance is less than zero, the account is in an
Overdrawn
state, and withdrawals are restricted. When the balance is greater than zero, the account is in a
Regular
state, and withdrawals are allowed.
Use Cases
The State
pattern is beneficial in several scenarios:
Document editing
: Different modes (insert, overwrite, select) with distinct behaviors.Vending machine
: Different states like idle, selecting, dispensing, and their associated actions.Workflow management
: Tasks change behavior based on whether they are "pending," "in progress," or "completed."Robotics
: Operational modes like "exploration" or "charging" that affect robot behavior.
Advantages
Positive
: Each state-specific behavior is isolated, making it easier to manage transitions and add new states.
Disadvantages
Negative
: Adding more states increases the number of classes, leading to more complexity.
Related Patterns
The State
pattern is closely related to several other design patterns:
Flyweight
: State objects can sometimes be shared across contexts, reducing memory usage.Singleton
: Often, state objects can be implemented as singletons since the state is shared.Strategy
: Similar to the state pattern, but strategies don't depend on internal state changes; they are chosen externally.Bridge
: Both patterns share the idea of composition to separate functionality, although they solve different problems.
Summary
The State
Pattern allows you to manage complex state-dependent behaviors
in
a clean and scalable way. By encapsulating states
in their own classes
,
you reduce
the complexity of conditional logic
and make the code
easier
to extend
.