Document directory
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
Package COM. bankht. state;/*** @ Author: -AK47 * @ Creation Time: 09:34:57 ** @ Class description: environment Angle class */public class context {// Private state of an object instance holding a state type; Public void setstate (State state) {This. state = State;}/*** interface method of interest to the user */Public void request (string sampleparameter) {// call state to process state. handle (sampleparameter );}}
Abstract status
Package COM. bankht. state;/*** @ Author: -AK47 * @ Creation Time: 09:35:29 ** @ Class description: abstract status class */public interface state {/*** processing corresponding to the State */Public void handle (string sampleparameter );}
Specific Status
Package COM. bankht. state;/*** @ Author: -AK47 * @ Creation Time: 09:35:52 ** @ Class description: specific Status class */public class concretestatea implements State {@ overridepublic void handle (string sampleparameter) {system. out. println ("concretestatea handle:" + sampleparameter );}}
Package COM. bankht. state;/*** @ Author: -AK47 * @ Creation Time: 09:36:15 ** @ Class description: */public class concretestateb implements State {@ overridepublic void handle (string sampleparameter) {system. out. println ("concretestateb handle:" + sampleparameter );}}
Client type
Package COM. bankht. state;/*** @ Author: -AK47 * @ Creation Time: 09:36:33 ** @ Class description: */public class client {public static void main (string [] ARGs) {// creation state = new concretestateb (); // create the environment context = new context (); // set the status to context in the environment. 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
Package COM. bankht. state. vote;/*** @ Author: -AK47 * @ Creation Time: 09:39:41 ** @ Class description: */public interface votestate {/*** process the behavior corresponding to the status ** @ Param user * voter * @ Param voteitem * voting item * @ Param votemanager * voting context, it is used to call back the Context Data */Public void vote (string user, string voteitem, votemanager) when the corresponding state function is implemented );}
Specific Status class-normal voting
Package COM. bankht. state. vote;/*** @ Author: -AK47 * @ Creation Time: 09:40:00 ** @ Class description: specific Status class-normal voting */public class normalvotestate implements votestate {@ overridepublic void vote (string user, string voteitem, votemanager) {// normal voting, record to the voting record votemanager. getmapvote (). put (user, voteitem); system. out. println ("congratulations, vote successful ");}}
Specific Status class-Repeated voting
Package COM. bankht. state. vote;/*** @ Author: -AK47 * @ Creation Time: 09:40:23 ** @ Class description: specific Status class -- Repeated voting */public class repeatvotestate implements votestate {@ overridepublic void vote (string user, string voteitem, votemanager) {// Repeated voting, not processing system temporarily. out. println ("Please do not vote again ");}}
Specific Status class-malicious ticket Flushing
Package COM. bankht. state. vote;/*** @ Author: -AK47 * @ Creation Time: 09:40:50 ** @ Class description: specific Status class-malicious ticket swiping */public class spitevotestate implements votestate {@ overridepublic void vote (string user, string voteitem, votemanager) {// the user's voting qualification is canceled, and cancel the voting record string STR = votemanager. getmapvote (). get (User); If (STR! = NULL) {votemanager. getmapvote (). Remove (User);} system. Out. println ("You have malicious screen swiping, vote canceled ");}}
Specific Status class-blacklist
Package COM. bankht. state. vote;/*** @ Author: -AK47 * @ Creation Time: 09:41:15 ** @ Class description: specific Status class-blacklist */public class blackvotestate implements votestate {@ overridepublic void vote (string user, string voteitem, votemanager) {// you cannot log on to the system in the blacklist. out. println ("Access to the blacklist will prohibit logon and use of the system ");}}
Environment
Package COM. bankht. state. vote; import Java. util. hashmap; import Java. util. map;/*** @ Author: -AK47 * @ Creation Time: 09:41:35 ** @ Class description: environment class */public class votemanager {// The Private votestate state of the handling object of the hold object = NULL; // record the user's voting result. Map <string, string> corresponds to the 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 user's voting result map */Public Map <string, string> getmapvote () {return mapvote ;} /*** vote ** @ Param user * voter * @ Param voteitem * vote option */Public void vote (string user, string voteitem) {// 1. increase the number of votes for the 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. determine the user's voting type, it is equivalent to determining whether the corresponding status is normal voting, repeated voting, malicious voting, or blacklist. // The following uses my own commonly-used "hypothesis correction" to complete the switch structure if (oldvotecount = 1) {state = new normalvotestate () ;}if (oldvotecount> 1) {state = new repeatvotestate () ;}if (oldvotecount> 5) {state = new spitevotestate ();} if (oldvotecount> 8) {state = new blackvotestate () ;}// 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 (); // call the State object to perform the corresponding operation state. vote (user, voteitem, this );}}
Client type
Package COM. bankht. state. vote;/*** @ Author: -AK47 * @ Creation Time: 09:42:06 ** @ Class description: client class */public class client {public static void main (string [] ARGs) {votemanager Vm = new votemanager (); For (INT I = 0; I <11; I ++) {VM. vote ("U1", "");}}}
The running result is as follows:
Congratulations, the vote is successful. Please do not vote repeatedly. You have malicious screen flushing. You are disqualified from voting for malicious screen flushing, if you cancel the voting qualification, you will have malicious screen flushing. If you cancel the voting qualification, you will not be allowed to log on to or use the System in the blacklist, and you will not be able to log on to or use the System in the blacklist, login and use of the system will be prohibited
As shown in the preceding example, the status transition is basically an internal action and is mainly maintained in the status mode. For example, the voting personnel always vote for their operations at any time, but the voting management object is not necessarily handled the same, and the status will be determined based on the number of votes, select different processing methods based on the status.
Recognition Status Mode
●Status and Behavior
The state of an object usually refers to the attribute value of an object instance, and behavior refers to the function of an object. Specifically, most actions can correspond to methods.
The function of state mode is to separate the state behavior and call different functions corresponding to different states by changing the maintenance state. That is to say, the status and behavior are associated, and their relationships can be described:Status determines Behavior.
Because the status is changed during the runtime, the behavior also changes during the runtime Based on the status change.
●Parallel Behavior
Pay attention to parallel lines rather than equality. The so-called parallelism refers to the behavior in different states at the same level. They are independent and unrelated, and decide which of the parallel lines is taken based on different states. Actions are different. Of course, their implementations are different, and they cannot be replaced.
Equality emphasizes the possibility of replacement. Different people describe or implement the same behavior. Therefore, when the same behavior occurs, you can select any implementation based on the conditions for corresponding processing.
You may find that the structure of the state mode is the same as that of the Policy mode, but their purpose, implementation, and nature are completely different. In addition, the characteristics between actions are also very important differences between the State mode and the policy mode. The state mode behaviors are parallel and cannot be replaced with each other. The behavior in the Policy mode is equal, yes.
●Environment and status processing object
In the state mode, the environment (context) is the object holding the State, but the environment (context) itself does not process state-related behaviors, instead, the processing status function is delegated to the status processing class corresponding to the status.
In a specific State Processing class, you often need to obtain the data of the Environment (context), and even call back the context method when necessary. Therefore) it is passed as a parameter to a specific status processing class.
Generally, the client only interacts with the environment (context. The client can use State objects to configure an environment (context). Once the configuration is complete, it no longer needs to deal with State objects. The client is generally not responsible for maintaining the status during running, nor deciding which specific State processing object to use in the future.