A state pattern is when the state of an object changes , Its behavior will also change

Usage :
(1), The behavior of an object depends on its state ( attribute ) And it can change its related behavior according to its state change .

(2), The code contains a large number of conditional statements related to the object state , The appearance of these conditional statements , It will lead to poor maintainability and flexibility of the code , It is not easy to add and delete states , The coupling between client class and class library is enhanced . The behavior of the object is included in these conditional statements , And these conditions correspond to various states of the object

such as :1 Orders , User orders , Business acceptance , Merchant delivery , Confirm receipt , The status of receiving goods successfully , Of course, users can also choose to cancel the order ( No sign in ) The state of

By implementing the same State Interface to interact to achieve decoupling

using System; using System.Collections.Generic; using System.Linq; using System
.Text; using System.Threading.Tasks; namespace State mode { public sealed class Order
{ private State currentState; public bool IsCancel { get; set; } public Order()
{ currentState = new WaintForAccept(); IsCancel = false; } private double minute
; public double Minute { get { return minute; } set { minute = value; } }
private bool finish; public bool Finish { get { return finish; } set { finish =
value; } } public void SetState(State s) { currentState = s; } public void
Action() { currentState.Process(this); } } public interface State { void Process
(Order order); } // Waiting for acceptance status - Specific status role public sealed class WaintForAccept : State {
public void Process(Order order) { Console.WriteLine(" We're going to take it , Start stock up ");
// The order can be cancelled within half an hour if (order.Minute < 30 && order.IsCancel) { Console.WriteLine(
" User cancels order "); order.SetState(new CancelOrder()); order.Action(); return; } order.
SetState(new AcceptAndDeliver()); order.Finish = false; order.Action(); } }
// Accept delivery public sealed class AcceptAndDeliver : State { public void Process(Order
order) { Console.WriteLine(" Stock up completed , We start shipping "); if (order.Minute < 30 && order.
IsCancel) { Console.WriteLine(" User cancels order "); order.SetState(new CancelOrder());
order.Action(); return; } if(order.Finish==false) { order.SetState(new
ConfirmReceipt()); order.Action(); } } } // Confirm receipt public sealed class
ConfirmReceipt : State { public void Process(Order order) { Console.WriteLine(
" Inspection of goods , No problem, I signed for it "); order.SetState(new Success()); order.Action(); } } // Successful delivery
public sealed class Success : State { public void Process(Order order) { Console
.WriteLine(" Harvest success "); order.Finish = true; } } // cancellation of order public sealed class
CancelOrder : State { public void Process(Order order) { Console.WriteLine(
" I don't like the goods , cancellation of order "); order.Finish = false; } } class Program { static void Main(string
[] args) { Order order = new Order(); order.IsCancel = true; order.Minute = 20;
order.Action(); Order order1 = new Order(); order1.IsCancel = false; order1.
Minute= 33; order1.Action(); Console.ReadKey(); } } } We're going to take it , Start stock up User cancels order
I don't like the goods , cancellation of order We're going to take it , Start stock up Stock up completed , We start shipping Inspection of goods , No problem, I signed for it Harvest success

Technology