When an object's internal state changes, it is allowed to change its behavior. When the object looks like it changes its class. State pattern can be realized: the behavior of an object depends on its state (property), and its related behavior can be changed according to its state change.
The state pattern solves the problem that the code contains a large number of conditional statements related to the object state.
using System; namespace StatePattern { class Program { static void Main(string[] args) { Context context = new Context(); context.State = new ConcreteState1(); context.Request(); context.Request(); context.Request(); context.Request(); context.Request(); context.Request(); context.Request(); context.Request(); context.Request(); } } public abstract class AbstractState { public abstract void Handler(Context context); } public class ConcreteState1 : AbstractState { public override void Handler(Context context) { context.State = new ConcreteState2(); } } public class ConcreteState2 : AbstractState { public override void Handler(Context context) { context.State = new ConcreteState3(); } } public class ConcreteState3 : AbstractState { public override void Handler(Context context) { context.State = new ConcreteState1(); } } public class Context { private AbstractState state; public AbstractState State { get { return state; } set { state = value; Console.WriteLine("Current status:", State.GetType().Name ); } } public void Request() { this.State.Handler(this); } } }
Test results:
using System; namespace StatePattern { /// <summary> ///State mode to display time greetings of qq mailbox /// </summary> class Program { static void Main(string[] args) { Context context = new Context(20); // Setting time Hour=20; context.State = new MorningState(); context.Request(); Console.WriteLine("----------------------"); context.Hour = 15; context.Request(); Console.WriteLine("----------------------"); context.Hour = 10; context.Request(); Console.WriteLine("----------------------"); } } public abstract class AbstractState { public abstract void Handler(Context context); } public class MorningState : AbstractState { public override void Handler(Context context) { if( context.Hour%24 > 9 && context.Hour%24 <= 12) { Console.WriteLine("Good morning"); } else { context.State = new AfternoonState(); context.Request(); } } } public class AfternoonState : AbstractState { public override void Handler(Context context) { if (context.Hour % 24 > 12 && context.Hour % 24 <= 18) { Console.WriteLine("Good afternoon"); } else { context.State = new EveningState(); context.Request(); } } } public class EveningState : AbstractState { public override void Handler(Context context) { if (context.Hour % 24 > 18 && context.Hour % 24 < 24) { Console.WriteLine("Good evening"); } else { context.State = new MorningState(); context.Request(); } } } public class Context { private int hour; public int Hour { get; set; } public Context(int nowHour) { this.Hour = nowHour; } private AbstractState state; public AbstractState State { get { return state; } set { state = value; Console.WriteLine("Current status:", State.GetType().Name ); } } public void Request() { this.State.Handler(this); } } }
Usually there is only one method in the interface of command mode. There are one or more methods in the state mode interface. In addition, the method of the implementation class of the state mode generally returns a value, or changes the value of the instance variable. In other words, the state pattern is generally related to the state of the object. The methods that implement the class have different functions, covering the methods in the interface. Like command mode, state mode can also be used to eliminate if else and other conditional selection statements.
https://m.runoob.com/design-pattern/state-pattern.html
https://www.bilibili.com/video/av78515440?p=16