Take advantage of the state design pattern to allow an object to change its behavior when its internal state changes Credit: tracyshaun Design patterns are used to solve common design problems and reduce the complexities in our source code. The state design pattern is a behavioral pattern that is used to represent a one to many relationship between objects in such a way that if one of the objects is modified, then all of the dependent objects are also modified. The Gang of Four defines the state design pattern as follows: Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. This article presents a discussion of how we can work with the state design pattern in .Net. When to use the state design pattern The state design pattern is typically needed when you have an object in your application that might pass through various phases. As an example, you might have an application that keeps track of the status of notifications that are sent via emails. At any given time, the status of such notifications could be any of five states: processed, delivered, opened, bounced, or undelivered. The participants in a representation of the state design pattern would typically include the following classes or objects: Context—the actual object to be accessed by the client. State—the interface (or maybe an abstract class) that encapsulates the behavior of the context. Concrete state—a class that represents the state of the context. This design pattern can be used when you have a one to many relationship between objects and if one of such objects is modified, all the dependent objects would be notified automatically. Implementing the state design pattern in C# As I said earlier, the state design pattern is used to represent the state of an object and then change the behavior of the object when the state is changed. Let me illustrate this design pattern by creating a simple console application in this next section. Create a console application project in Visual Studio by following the steps given below. In the Visual Studio IDE, click on File -> New -> Project. Select “Console App (.Net Framework)” as the template of choice. Specify a name for your project. Click OK. The following code snippet illustrates an abstract class called State. It contains an abstract method called Process that accepts a reference to an instance of the Context class. We’ll talk about the Context class later. public abstract class State { public abstract void Process(Context context); } The following two classes, ConcreteStateOne and ConcreteStateTwo, extend the State class and implement the Process method. public class ConcreteStateOne : State { public override void Process(Context context) { context.State = new ConcreteStateTwo(); } } public class ConcreteStateTwo : State { public override void Process(Context context) { context.State = new ConcreteStateOne(); } } The Context class is shown below. This class holds an instance of concrete state, meaning it defines the current state. The Context class contains an argument constructor that accepts a reference to an instance of the State class as an argument. It also contains a method named Toggle that when called changes the state of the object. public class Context { public Context(State state) { this.State = state; } public State State { get; set; } public void Toggle() { State.Process(this); } } Lastly, here is how you can create an instance of the Context class and then invoke the Toggle method to change the state of the object. static void Main(string[] args) { Context context = new Context(new ConcreteStateOne()); context.Toggle(); context.Toggle(); Console.Read(); } Related content feature What is Rust? Safe, fast, and easy software development Unlike most programming languages, Rust doesn't make you choose between speed, safety, and ease of use. Find out how Rust delivers better code with fewer compromises, and a few downsides to consider before learning Rust. By Serdar Yegulalp Nov 20, 2024 11 mins Rust Programming Languages Software Development how-to Kotlin for Java developers: Classes and coroutines Kotlin was designed to bring more flexibility and flow to programming in the JVM. Here's an in-depth look at how Kotlin makes working with classes and objects easier and introduces coroutines to modernize concurrency. By Matthew Tyson Nov 20, 2024 9 mins Java Kotlin Programming Languages analysis Azure AI Foundry tools for changes in AI applications Microsoft’s launch of Azure AI Foundry at Ignite 2024 signals a welcome shift from chatbots to agents and to using AI for business process automation. By Simon Bisson Nov 20, 2024 7 mins Microsoft Azure Generative AI Development Tools news Microsoft unveils imaging APIs for Windows Copilot Runtime Generative AI-backed APIs will allow developers to build image super resolution, image segmentation, object erase, and OCR capabilities into Windows applications. By Paul Krill Nov 19, 2024 2 mins Generative AI APIs Development Libraries and Frameworks Resources Videos