State Design Pattern

State Design Pattern

18 Jun 2024
Intermediate
51.8K Views
2 min read
Learn via Video Course & by Doing Hands-on Labs

⭐ .NET Design Patterns Course: Design Patterns in C# Online Training

State Design Pattern falls under Behavioral Pattern of Gang of Four (GOF) Design Patterns in .Net. The command pattern is commonly used in the menu systems of many applications such as Editor, IDE, etc. In this article, I would like to share what is state pattern and how is it work?

What is State Design Pattern?

This pattern is used when there are one too many relationships between objects such as if one object is modified, its dependent objects are to be notified automatically. State Design Pattern is used to alter the behavior of an object when it’s internal state changes. In this pattern, an object is created which represent various states and a context object whose behavior varies as it's state object changes.

This pattern seems like a dynamic version of the Strategy pattern.

State Design Pattern - UML Diagram & Implementation

The UML class diagram for the implementation of the State Design Pattern is given below:

State Design Pattern C#

The classes, interfaces, and objects in the above UML class diagram are as follows:

  1. Context

    This is a class that holds a concrete state object that provides the behavior according to its current state. This is used by the clients.

  2. State

    This is an interface that is used by the Context object to access the changeable functionality.

  3. ConcreteStateA/B

    These are classes that implement State interface and provide the real functionality that will be used by the Context object. Each concrete state class provides behavior that is applicable to a single state of the Context object.

C# - Implementation Code

public class Context
{
 private IState state;

 public Context(IState newstate)
 {
 state = newstate;
 }

 public void Request()
 {
 state.Handle(this);
 }

 public IState State
 {
 get { return state; }
 set { state = value; }
 }
}


public interface IState
{
 void Handle(Context context);
}


public class ConcreteStateA : IState
{
 public void Handle(Context context)
 {
 Console.WriteLine("Handle called from ConcreteStateA");
 context.State = new ConcreteStateB();
 }
}

public class ConcreteStateB : IState
{
 public void Handle(Context context)
 {
 Console.WriteLine("Handle called from ConcreteStateB");
 context.State = new ConcreteStateA();
 }
} 


Real Life Example:

Real Life Example of State Design Pattern C#

When to use it?

  1. The behavior of an object is changed based on its state.

  2. Preserve flexibility in assigning requests to handlers.

  3. An object is becoming complex, with many conditional behaviors.

Read More Articles Related to Design patterns
What do you think?

I hope you will enjoy the State Design Pattern while designing your software. I would like to have feedback from my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

FAQs

Q1. Can the State Design Pattern be implemented without a State interface?

 Yes, it can be done without an interface, but having a common interface helps maintain consistency and flexibility when adding new states. 

Q2. How does the State Pattern differ from the Strategy Pattern?

 The State Pattern changes behavior based on an internal state, while the Strategy Pattern allows clients to choose behaviors without state dependency. 

Q3. Is it necessary to define all possible states upfront?

 No, states can be added incrementally as needed during system development. 

Q4. Can states share common behavior in the State Pattern?

 Yes, common behavior can be placed in a base class to reduce code duplication among concrete states. 

Q5. Does the State Pattern impact performance?

 While the pattern improves maintainability, the use of multiple-state objects may slightly affect performance due to object creation and transition handling. 

Q6. What is the main purpose of the state design pattern?

The main purpose of the State Design Pattern is to allow an object to change its behavior when its internal state changes. This promotes cleaner code by encapsulating state-specific behavior, making the system more flexible and maintainable.

Q7. Which design pattern should we use for state management?

The State Design Pattern is ideal for state management when an object's behavior changes in response to its internal state. It simplifies complex state transitions by separating state-specific logic into distinct classes.

Q8. What are the consequences of state design pattern?

The State Design Pattern might result in an increase in the number of classes because each state is represented by its own class. While it improves flexibility and maintainability, it can also increase system complexity by managing many state classes and transitions.
Share Article

Live Classes Schedule

Our learn-by-building-project method enables you to build practical/coding experience that sticks. 95% of our learners say they have confidence and remember more when they learn by building real world projects.
ASP.NET Core Certification TrainingSep 15SAT, SUN
Filling Fast
09:30AM to 11:30AM (IST)
Get Details
Software Architecture and Design TrainingSep 22SAT, SUN
Filling Fast
07:00AM to 09:00AM (IST)
Get Details
ASP.NET Core Certification TrainingSep 29SAT, SUN
Filling Fast
08:30PM to 10:30PM (IST)
Get Details

Can't find convenient schedule? Let us know

About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Accept cookies & close this