How to use the state design model in .Net

0


Design patterns are used to solve common design problems and reduce the complexities of our source code. The state design model is a behavioral model used to represent a one-to-many relationship between objects in such a way that if one of the objects is changed, all dependent objects are also changed.

The Gang of Four sets the state’s design pattern as following:

Allow an object to change its behavior when its internal state changes. The object will appear to change class.

This article presents a discussion of how we can work with the state design model in .Net.

When to use the report design template

The state design model is typically needed when you have an object in your application that can go through different phases. For example, you can have an app that keeps track of the status of notifications sent by email. At any time, the status of these notifications can be one of five states: processed, delivered, opened, rejected, or undelivered.

Participants in a state design model representation would typically include the following classes or objects:

  • Context: the real object to which the client must access.
  • State — the interface (or perhaps 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 any of these objects change, all dependent objects will be automatically notified.

Implementing the state design model in C #

As I said earlier, the state design model is used to represent the state of an object and then to change the behavior of the object when the state is changed. Let me illustrate this design pattern by creating a simple console app in this next section.

Create a console application project in Visual Studio by following the steps below.

  1. In the Visual Studio IDE, click File -> New -> Project.
  2. Select “Console App (.Net Framework)” as the template of choice.
  3. Specify a name for your project.
  4. Click on OK.

The following code snippet demonstrates an abstract class called State. It contains an abstract method called Process which 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 next 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 contains a concrete state instance, which means that 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 which, 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);
        }
    }

Finally, here’s how to create an instance of the Context class, 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();
        }

Copyright © 2018 IDG Communications, Inc.


Share.

Leave A Reply