[review]Design Pattern:State
State
Alter the object's behavior by the changes of its state. And the states are all encapsulated into single object state by state. decouple the state(especially the object's behavior under different states) from the the object itself
When we use this pattern
They are all about the state of the object and dependency on the state, we are highly recommended to use this pattern if below:
- The behavior of the object depends on its states so much, the behavior will be changed while the changes of the states
- If we got lots of if/else statements to describe different behaviors under different conditions. By the help of this pattern, we can design the each statement as a very concrete state object. If we got lots of if/else statements, our method may be very large, which is not what we expected and is ugly. we should avoid this situationa and try our best to reduce the using so many if/else statements.
Roles in this pattern
- Content: This is the very object that depends so much on its states. this object defines the interface that the clients want. and also maintains an instance of the ConcreteState which represents the current state of the Content
- State: this is interface designed for the particular behavior depending on the particular state
- ConcreteState: a object which represents an particular state and also implements the State interface for the particular bahavior, the amount of the ConcreteState depends on the amount of Content's state.
One more thing for the ConcreteState: which not only implements the particular behavior of the content but also is responsible fot setting the content's state from the current(itself) to the next.
Easy to say but hard to do, a small example below to demonstrate the above:
View Code
namespace State
{
public abstract class State
{
public abstract void Handle(Content content);//implement the behavior but also change the state to next
}
public class ConcreteStateA : State
{
public override void Handle(Content content)
{
Console.WriteLine("ConcreteStateA.Handle");//implement the behavior
content.State = new ConcreteStateB();//change the state, the next state of this state is state B
}
}
public class ConcreteStateB : State
{
public override void Handle(Content content)
{
Console.WriteLine("ConcreteStateB.Handle");//implement the behavior
content.State = new ConcreteStateA();////change the state, the next state of this state is state A
}
}
public class Content
{
private State state;
public Content(State state)
{
this.state = state;
}
/// <summary>
/// default constructor is here to set the default state if no special state is asked when initialization
/// </summary>
public Content()
{
this.state = new ConcreteStateA();
}
public State State
{
get { return this.state; }
set { this.state = value; }
}
public void Request()
{
state.Handle(this);
}
}
class Program
{
static void Main(string[] args)
{
Content content = new Content();//use the default constructor
content.Request();
content.Request();
content.Request();
}
}
}
Different hehaviors under different states, the behavior is implemented by the corresponding state which also changes the state from the current to the next.
The content maintains the instance of an state and its behaviors are changed by the changes of its states. But they are decoupled from each other .
The state is self-described.
The one who always maintains the instance of State is the state.
The state is the state which belongs to the Content but not others.
