State Design Pattern

Introduction
 
The State Pattern (also referred to as State Design Pattern) is one of the behavioral patterns. It defines a manner for controlling communication among classes or entities. It is used to change the behavior of the object when the object's internal state is changed. This pattern helps objects to change it state without changing the interface (used to access the object) or lose the current state. This change of the state of the class or object is hidden from the outer world with the use of a context (also known as wrapper object). The State Pattern can be used to implement complex decision-making algorithms represented as a flow chart or state diagram. A State Pattern can also be implement where various states exist and each state has a specific action.
 
Structure
 
The UML Class Diagram of the State Pattern is as below.

State-Design-Pattern-1.jpg
 
This pattern has three main parts, context, statebase and concrete state (State A, State B, State C and so on). The Context is also called Client. Client does not access any state directly. It only holds a concrete state object that provides the current behavior of the context object. StateBase is the base class of all concrete states. It may be an abstract class or an interface. Concrete class (State A, State B and State C) is provided functionality that will be used for change and hold the behavior of the context object.
 
The basic code structure of the State Design Pattern implementation uses C# as in the following. Here starting state of context is passed through a constructor. A "Request" method of Context is used to change the current state to the next state. Internally it calls the StateBase.Chage() method. The StateBase.Chage() method switches the state of the passed context object to the next state. In this example the state change sequence is as below.

State-Design-Pattern-2.jpg
 
State Code

public interface StateBase

{

    void Change(Context context);

}

 

public class StateA : StateBase

{

    public void Change(Context context)

    {

        //Change state of context from A to B.

        context.State = new StateB();

        Console.WriteLine("Change state from A to B.");

    }

}

 

public class StateB : StateBase

{

    public void Change(Context context)

    {

        //Change state of context from B to C.

        context.State = new StateC();

        Console.WriteLine("Change state from B to C.");

    }

}

 

public class StateC : StateBase

{

    public void Change(Context context)

    {

        //Change state of context from C to A.

        context.State = new StateA();

        Console.WriteLine("Change state from C to A.");

    }

}

Client Code

public class Context

{

    public Context(StateBase state)

    {

        State = state;

        Console.WriteLine("Create object of context class with initial State.");

    }

 

    public StateBase State { get; set; }

 

    /// <summary>

    /// State change request

    /// </summary>

    public void Request()

    {

        State.Change(this);

    }

}
 
Sample code and output
 

static void Main(string[] args)

{

    // create Context object and suplied current state or initial state (state A).

    Context context = new Context(new StateA());

 

    //Change state request from A to B.

    context.Request();

 

    //Change state request from B to C.

    context.Request();

 

    //Change state request from C to A.

    context.Request();

}

State-Design-Pattern-3.jpg

Conclusion
 
The State Design Pattern enables us to alter an object's behavior when it's internal state changes. Using the State Design Pattern, it is much easier to track all the possible condition s(or states) and this ensures that our application will have less undefined behavior.  There are also many other ways to implement a State Pattern. The design of an effective State Pattern requires the designer to make a list of possible states and relate each state with each other or we can say define a sequence of states to be changed.

Hope this will help.