Please enable Javascript to correctly display the contents on Dot Net Tricks!

State Design Pattern - C#

  Author : Shailendra Chauhan
Posted On : 23 Feb 2017
Total Views : 1,390   
Updated On : 23 Feb 2017
 

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 share what is state pattern and how is it work?

What is State Design Pattern?

This pattern is used when there is one to many relationship between objects such as if one object is modified, its dependent objects are to be notified automatically.This 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 as 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 implements 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.

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.



ABOUT AUTHOR

Shailendra Chauhan
Author, Architect, Corporate Trainer and Microsoft MVP

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers, ASP.NET MVC Interview Questions and Answers, AngularJS Interview Questions and Answers and LINQ Interview Questions and AnswersKnow more...

Free Interview Books
 
COMMENTS (0)
23 MAY
Xamarin Forms : Build Cross-platform Apps (Online)

09:00 PM-11:00 PM IST(+5.30 GMT)

Know More
5 MAY
ASP.NET MVC with Angular4 Development (Online)

06:30 AM-08:30 AM IST(+5.30 GMT)

Know More
29 APR
Xamarin Forms : Build Cross-platform Apps (Classroom)

5:00 PM-6:30 PM

Know More
29 APR
ASP.NET MVC with Angular2 Development (Classroom)

9:30 AM-11:00 AM

Know More
27 APR
ASP.NET Core Development (Online)

07:00 AM - 09:00 AM IST( TUS, THR)

Know More
22 APR
NodeJS Development (Classroom)

11:00 AM-12:30 PM

10 APR
Master Class ASP.NET MVC 5 with Angular2 Plus Angular4 Development (Online)

09:00 PM - 11:00 PM IST (M/W/F)

8 APR
MEAN Stack 2 Development (Classroom)

11:00 AM-12:30 PM

25 MAR
Xamarin Forms : Build Cross-platform Apps (Classroom)

8:00 AM-9:30AM

21 MAR
ASP.NET MVC with Angular2 Development (Online)

09:00 PM - 11:00 PM IST( TUS, THR, SAT)

25 FEB
ASP.NET MVC with Angular2 Development (Classroom)

03:30 PM-05:30 PM

24 FEB
Angular 2 with Type Script (Online)

09:00 PM-11:00 PM IST(Mon,Wed,Fri)

BROWSE BY CATEGORY
 
 
LIKE US ON FACEBOOK
 
+