State Pattern in C# – Real-Time Example (Behavior Based on State)
What is State Pattern?
The State Pattern is a behavioral design pattern where an object changes its behavior when its internal state changes.
👉 The object behaves differently depending on its current state.
It is similar to a finite state machine.
Why Use State Pattern?
- Remove large if-else/switch statements
- Organize state-specific behavior
- Improve maintainability
- Make state transitions cleaner
- Follow Open/Closed Principle
Real-Time Scenario
Order Processing System:
- Pending
- Paid
- Shipped
- Delivered
- Cancelled
👉 Order behavior changes based on current status.
Real-Time Example – Order State Workflow
Step 1: State Interface
public interface IOrderState
{
void Handle();
}
Step 2: Concrete States
Pending State
public class PendingState : IOrderState
{
public void Handle()
{
Console.WriteLine("Order is Pending");
}
}
Paid State
public class PaidState : IOrderState
{
public void Handle()
{
Console.WriteLine("Order is Paid");
}
}
Shipped State
public class ShippedState : IOrderState
{
public void Handle()
{
Console.WriteLine("Order is Shipped");
}
}
Step 3: Context Class
public class OrderContext
{
private IOrderState _state;
public void SetState(IOrderState state)
{
_state = state;
}
public void Process()
{
_state.Handle();
}
}
Usage Example
var order = new OrderContext();
order.SetState(new PendingState());
order.Process();
order.SetState(new PaidState());
order.Process();
order.SetState(new ShippedState());
order.Process();
Output
Order is Pending
Order is Paid
Order is Shipped
Key Concept
Instead of:
if(status == "Pending")
{
}
else if(status == "Paid")
{
}
else if(status == "Shipped")
{
}
We do:
SetState(new PaidState());
👉 Behavior changes dynamically.
Diagram Understanding
OrderContext
↓
IOrderState
↓
-------------------------
PendingState
PaidState
ShippedState
👉 Each state has separate behavior.
Advantages
- ✔ Removes complex conditions
- ✔ Cleaner state management
- ✔ Easier to extend new states
- ✔ Better maintainability
- ✔ Encapsulates state behavior
Disadvantages
- ✖ More classes
- ✖ Can increase complexity
- ✖ State transitions need careful management
When to Use
Use State Pattern when:
- Object behavior changes based on state
- Large conditional logic exists
- Workflow/state transitions are complex
- States change dynamically
Real Project Mapping (.NET + Angular)
| Feature | Usage |
|---|---|
| Order workflow | State |
| Payment lifecycle | State |
| Ticket status | State |
| User authentication flow | State |
| Document approval workflow | State |
ASP.NET Core Real Example
Authentication State:
- Unauthenticated
- Authenticated
- Locked
- Expired
👉 User behavior changes by state.
Advanced Example – Payment System
Pending
↓
Processing
↓
Success / Failed
👉 Each state handles logic differently.
State vs Strategy
| State | Strategy |
|---|---|
| Changes behavior by internal state | Changes behavior by selected algorithm |
| State transitions important | Algorithms independent |
| Workflow-oriented | Behavior-oriented |
Pro Tip
State Pattern works well with:
- Workflow engines
- Finite state machines
- CQRS workflows
- Event-driven systems
- Saga orchestration
Summary
State Pattern helps you:
- Manage state-based behavior cleanly
- Remove large conditional blocks
- Build scalable workflows
👉 Perfect for:
- Order systems
- Payment processing
- Approval workflows
- Authentication systems
Comments
Post a Comment