ArticleDirectory
He first described the State mode in the book "Java and mode" by Dr. Yan Hong:
Status mode, also known as pattern of objects for states, is the behavior mode of an object.
The State mode allows an object to change its behavior when its internal state changes. This object looks like a class that has changed it.
Status mode structure
In one sentence, the State mode encapsulates the behavior of the object to be studied in different State objects. Each State object belongs to a subclass of an abstract state class. The intention of the state mode is to change the behavior of an object when its internal state changes. The schematic diagram of the Status mode is as follows:
The following roles are involved in the status mode:
● The Environment (context) role is also a context: defines the interface that the client is interested in and retains an instance of a specific status class. The instance of this specific status class shows the existing status of the environment object.
● Abstract state role: defines an interface to encapsulate the behavior of a specific State of an environment (context) object.
● A concretestate role: Each state class implements a behavior corresponding to a state in the context.
Source code
Environment
Public Class Context { // Hold an object instance of the State type Private State state; Public Void Setstate (State state ){ This . State = State ;} /** * Interface methods of interest to users */ Public Void Request (string sampleparameter ){ // Call state to process State. Handle (sampleparameter );}}
Abstract status
Public InterfaceState {/*** Processing corresponding to the status*/Public VoidHandle (string sampleparameter );}
Specific Status
Public class concretestatea implements state {@ override Public void handle (string sampleparameter) {system. out. println ( "concretestatea handle:" + sampleparameter) ;}
Public ClassConcretestatebImplementsState {@ overridePublic VoidHandle (string sampleparameter) {system. Out. println ("Concretestateb handle:" +Sampleparameter );}}
Client type
Public Class Client { Public Static Void Main (string [] ARGs ){ // Creation status State state = New Concretestateb (); // Create Environment Context context = New Context (); // Set the status to the environment Context. setstate (State ); // Request Context. Request ("test" );}}
From the above, we can see that the action request () of the context class is delegated to a specific State class. By using the polymorphism principle, You can dynamically change the State content of the context attribute of the Environment class, so that it changes from pointing to a specific State class to pointing to another specific state class, so that the environmental behavior request () it is executed by different status classes.
Use Cases
Considering the application of an online voting system, to control a single user, you can only vote for one vote. If a user repeatedly votes and votes more than five times, it is determined that the vote is malicious, to cancel the user's voting qualification, of course, the user's vote should also be canceled; if a user votes more than eight times, the user will be blacklisted and prohibited from logging on to and using the system.
To use the status mode, you must first define the various states of the voting process. According to the preceding descriptions, the voting process can be divided into four states: normal voting, repeated voting, malicious vote collection, and blacklist. Create a voting management object (equivalent to context ).
The system structure is as follows:
Source Code
Abstract status
Public InterfaceVotestate {/*** Action corresponding to processing status *@ ParamUser voter *@ ParamVoteitem voting item *@ ParamVotemanager voting context, which is used to call back the context data when the corresponding state function is implemented.*/Public VoidVote (string user, string voteitem, votemanager );}
Specific Status class-normal voting
Public ClassNormalvotestateImplementsVotestate {@ overridePublic VoidVote (string user, string voteitem, votemanager ){//Normal voting, recorded in the voting recordVotemanager. getmapvote (). Put (user, voteitem); system. Out. println ("Congratulations! Vote successful");}}
Specific Status class-Repeated voting
Public ClassRepeatvotestateImplementsVotestate {@ overridePublic VoidVote (string user, string voteitem, votemanager ){//Repeat the vote and will not be processed for the momentSystem. Out. println ("Please do not vote again");}}
Specific Status class-malicious ticket Flushing
Public ClassSpitevotestateImplementsVotestate {@ overridePublic VoidVote (string user, string voteitem, votemanager ){//Malicious voting, canceling user voting qualification, and canceling voting recordsString STR =Votemanager. getmapvote (). Get (User );If(STR! =Null) {Votemanager. getmapvote (). Remove (User);} system. Out. println ("You have malicious screen flushing and are disqualified from voting");}}
Specific Status class-blacklist
Public ClassBlackvotestateImplementsVotestate {@ overridePublic VoidVote (string user, string voteitem, votemanager ){//Logging blacklist. logon to the system is prohibited.System. Out. println ("Access to the blacklist will disable logon and use of the system");}}
Environment
Public Class Votemanager { // Processing object of the holding body Private Votestate state = Null ; // Record the user's voting result. Map <string, string> corresponds to map <user name, voting options> Private Map <string, string> mapvote = New Hashmap <string, string> (); // Record the number of user votes, Map <string, integer> corresponding map <user name, number of votes> Private Map <string, integer> mapvotecount = New Hashmap <string, integer>(); /** * Obtain the map of the user's voting result */ Public Map <string, string> Getmapvote (){ Return Mapvote ;} /** * Vote * @ Param User voter * @ Param Voteitem voting options */ Public Void Vote (string user, string voteitem ){ // 1. Increase the number of votes for this user // Retrieve the user's existing votes from the record Integer oldvotecount = Mapvotecount. Get (User ); If (Oldvotecount = Null ) {Oldvotecount = 0 ;} Oldvotecount + = 1; Mapvotecount. Put (user, oldvotecount ); // 2. Determining the user's voting type is equivalent to determining the corresponding status // Whether it is normal voting, repeated voting, malicious voting, or blacklisted status If (Oldvotecount = 1 ) {State = New Normalvotestate ();} Else If (Oldvotecount> 1 & oldvotecount <5 ) {State = New Repeatvotestate ();} Else If (Oldvotecount> = 5 & oldvotecount <8 ) {State = New Spitevotestate ();} Else If (Oldvotecount> 8 ) {State = New Blackvotestate ();} // Then, you can call the status object to perform corresponding operations. State. Vote (user, voteitem, This );}}
Client type
Public ClassClient {Public Static VoidMain (string [] ARGs) {votemanager VM=NewVotemanager ();For(IntI = 0; I <9; I ++) {VM. Vote ("U1", "");}}}
The running result is as follows: