State mode
State mode is the twin brother of the strategy mode, the state mode emphasizes the change of the object's behavior, the policy mode is the external choice of the policy, and the choice of the policy is determined by the external condition.
Environment Class (context): Defines the interface to which the customer is interested. Maintains an instance of the Concretestate subclass that defines the current state.
Abstract State Class: Defines an interface to encapsulate behavior related to a particular state of a context.
Specific state classes (Concretestate): Each subclass implements a behavior related to a state of the context.
We use the company's application approval process for example, the first is the staff to make an application, and then transfer to the supervisor, manager, director.
First, define an abstract state class interface
1 Public Interfaceistate2 {3 /// <summary>4 ///Application5 /// </summary>6 /// <param name= "Vac" ></param>7 voidSubmit (vacationapply vac);8}
Defining environment classes (context)
1 Public classstaffapply2 {3 Publicistate currentapplystate;4 5 Publicstaffapply (istate vs)6 {7 This. Currentapplystate =vs;8 }9 Ten Public voidSubmit () One { ACurrentapplystate.submit ( This); - } -}
Defining a specific State Class (Concretestate)
1 /// <summary>2 ///Employees3 /// </summary>4 Public classstaffstate:istate5 {6 Public voidSubmit (staffapply vac)7 {8Console.WriteLine ("the current process has been to employees ...");9Vac. Currentapplystate =Newsupervisorstate ();Ten } One } A - /// <summary> - ///Supervisor the /// </summary> - Public classsupervisorstate:istate - { - + Public voidSubmit (staffapply vac) - { +Console.WriteLine ("the current process has reached the supervisor ..."); AVac. Currentapplystate =Newmanagerstate (); at } - } - - /// <summary> - ///Manager - /// </summary> in Public classmanagerstate:istate - { to Public voidSubmit (staffapply vac) + { -Console.WriteLine ("the current process has reached the manager ..."); theVac. Currentapplystate =Newdirectorstate (); * } $ Panax Notoginseng } - the /// <summary> + ///Director A /// </summary> the Public classdirectorstate:istate + { - Public voidSubmit (staffapply vac) $ { $Console.WriteLine ("the current process has reached the Director ..."); -Vac. Currentapplystate =Newdirectorstate (); - } the}
You can see that the next state of the current specific State class is determined by the class itself. is transparent to the outside. To be submitted by the supervisor's application.
Vac. Currentapplystate = new Managerstate ();
The next state of the supervisor has decided that his next state is to the manager. This step can be modified according to business logic
The final client code is as follows
1 Static voidMain (string[] args)2 {3 //start an employee application4Staffapply VOC =NewStaffapply (Newstaffstate ());5 //Submit Next6 VOC. Submit ();7 console.readline ();8 //Submit Next9 VOC. Submit ();Ten console.readline (); One //Submit Next A VOC. Submit (); - console.readline (); - //Submit Next the VOC. Submit (); - console.readline (); -}
The results are as follows:
State mode in C # design mode