State Design Pattern

State Design Pattern

29 Mar 2024
Intermediate
50.9K 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.

Share Article
Batches Schedule
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.
Self-paced Membership
  • 22+ Courses
  • 750+ Hands-On Labs
  • 300+ Quick Notes
  • 55+ Skill Tests
  • 45+ Interview Q&A
  • 10+ Real-world Projects
  • Career Coaching
  • Email Support
Upto 66% OFF
KNOW MORE..

To get full access to all courses

Accept cookies & close this