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 14 great preprocessors for developers who love to code Sometimes it seems like the rules of programming are designed to make coding a chore. Here are 14 ways preprocessors can help make software development fun again. By Peter Wayner Nov 18, 2024 10 mins Development Tools Software Development feature Designing the APIs that accidentally power businesses Well-designed APIs, even those often-neglected internal APIs, make developers more productive and businesses more agile. By Jean Yang Nov 18, 2024 6 mins APIs Software Development news Spin 3.0 supports polyglot development using Wasm components Fermyon’s open source framework for building server-side WebAssembly apps allows developers to compose apps from components created with different languages. By Paul Krill Nov 18, 2024 2 mins Microservices Serverless Computing Development Libraries and Frameworks news Go language evolving for future hardware, AI workloads The Go team is working to adapt Go to large multicore systems, the latest hardware instructions, and the needs of developers of large-scale AI systems. By Paul Krill Nov 15, 2024 3 mins Google Go Generative AI Programming Languages Resources Videos