Author - StudySection Post Views - 269 views
State Design

State Design Pattern

The state model (also known as the state design model) is one of the behavior models. It defines a way to control communication between classes or entities. It is used to modify the behavior of the object when the internal state of the object is changed. The pattern helps objects change state without changing the interface (used to access the object) or losing the current state. This change in the state of the class or object is masked from the outside world using a context (also known as a wrapper object). The state model can be used to implement complex decision algorithms represented as a flowchart or state diagram. A state model can also be implemented when there are different states and each state has a specific action.

Structure

The Diagram of the State Pattern:
flow-chart
StateBase is the base class for all concrete states. It can be an abstract class or an interface The concrete class (State A, State B, and State C) has characteristics that can be used to modify and maintain the behavior of the context object.
flow-chart1

The basic code structure of the state design model implementation uses C #. the initial state of the context is passed through a constructor. A Context “Request” method is used to change states internally, it is called the StateBase.Chage () method. The StateBase.Chage () method changes the state of the context object to the next state. In this example, the state change sequence is as follows.

The State Design Pattern allows us to modify the behavior of an object when its internal state changes. Using the State Design Pattern makes it much easier to keep track of all possible conditions (or states) and this ensures that our application will behave less undefined. There are also many other ways to implement a state model. Designing an effective state model requires the designer to make a list of possible states and relate each state to each other or it can be said to define a sequence of states to be modified.
static void Main(string[] args)
{
// create a Context object and supply the current state or initial state (state A).
Context context = new Context(new StateA());
//Change state requests from A to B.
context.Request();
//Change state requests from B to C.
context.Request();
//Change state requests from C to A.
context.Request();
}
public interface StateBase
{
void ChangeState(Context context);
}
public class StateA: StateBase
{
public void ChangeState(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 ChangeState(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 ChangeState(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 ChangeState(StateBase state)
{
State = state;
Console.WriteLine("Create object of context class with first State.");
}
public StateBase State { get; set; }
/// <summary>
/// State change request
/// </summary>
public void Request()
{
State.Change(this);
}
}

The English language is the most widely used language as a medium of communication around the world. Having a certification for the English language can be an advantage. StudySection provides an English Certification Exam that tests English language proficiency in English grammar, reading, and writing.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri