State definition: Different states, different behaviors; or, each state has the corresponding behavior.
When is it used? State mode is more in practical use, suitable for "Switching". Because we often use if elseif else to perform status switching, if this judgment switching is repeated, we will be associated with the State mode.
Not just according to the state, there is also an attribute. If the properties of an object are different, the behavior of the object is different, this is relatively high in the database system, and we often in the tail of a data sheet, plus the property property meaning, to identify records Some special nature records, this attribute change (switching) is at any time, it is possible to use State.
Is it used? In actual use, the same status switch is much smuggling, but sometimes it is not so obvious, depending on your experience and the understanding of the system.
Here, it is to be described here that "switch switching state" and "general state judgment" are some differences, "general state judgment" is also an IF..EELSEIF structure, for example:
IF (which == 1) state = "hello"; else if (which == 2) state = "hi"; Else if (Which == 3) state = "bye";
This is a "general state judgment", and the State value is different from the Which variable, which is not related to the WHICH and STATE. If it is changed:
IF (state.euqals ("bye")) "" Hello "; Else IF (" Hello ")) State =" hi "; Else IF (state.euqals (" hi ")))) State =" BYE ";
This is "switching switch state", is to switch the status from "Hello" to "Hi", and then switch to "" bye "; switch to" Hello ", as an rotary switch, this state change Use the State mode.
If you are simply handover "Hello" -> "Hi" -> "BYE" -> "Hello", it is not necessary to use the State mode, because the State mode creates a lot of subcategories, complex Chemical, but if another behavior occurs: switch the above switching direction, or if you need any switch, you will need State.
Please see the case:
Public class context {private color state = null; public void push () {// If the current RED state is switched to Blue if (state == color.red) state = color.blue; // If the current Blue status is switched to Green else if (state == color.blue) state = color.green; // If the current BLACK state is switched to red else if (state == color.black) state = color.red; // If the current GREEN status is Switch to Black Else if (state == color.green) state = color.ble; Sample Sample = new sample (state); sample.operate ();} public void pull () {// and PUSH status to switch exact opposite IF (state == color.green) state = color.blue; Else if (state == color.black) state = color.green; else if (state == color.blue) state = color.red; else f == color.red) state = color.ble; sample2 Sample2 = New Sample2; Sample2.Operate ();}} In the previous example, we have two action Push push and pull pull, these two switching action Change the context color, and we need to optimize it using the State mode.
Also pay attention to: But on the above example, State changes, only simple color assignment, this specific behavior is very simple, State is suitable for huge specific behavior, so in this example, it is not necessarily necessary to use State. Mode, this increases the number of subclasses, and simpler is complex.
For example: bank accounts often convert between open states and CLOSE states.
For example: Classic TCPConnection, TCP state has creation listening to shut three, and repeated conversion, the specific behavior that creates a listening close to the simple one can be completed, suitable for use in STATE
For example: the mailbox POP account, there will be four states, and Start HaveUserName Authorized Quit, each state corresponds to the behavior should be relatively large. Suitable for using State
For example: Pick different tools in the toolbox can be seen as switches in different tools, suitable for use in STATE. If a specific drawing program, the user can choose a different tool to draw a square frame line curve, and this state switch can be used.
How to use State requires two types of entities:
1.State Manager Status Manager is the switch, and the context actually actually a State Manager in the STATE Manager in State Manager. 2. Different status is the different subcategories of inheriting this parent class with the parent category of abstract categories or interface.
Take the context above as an example. We want to modify it and build two types of entities. Step 1: First create a parent category:
Public Abstract Class State {Public Abstract Void Handlepush (Context C); Public Abstract Void Handlepull (CONTEXT C); Public Abstract Void getColor ();
The method in the parent category should correspond to the switch behavior in State Manager, in this case in State Manager, there are two switching action Push push and PULL pull. Then there is a specific processing of these two actions in the status parent category: handlepush () handlepull (); simultaneously requires a method of getting the PUSH or PULL results getColor () below is the implementation of specific subcategories:
Public Class Bluestate Extends State {public void handlepush (Context C) {// According to PUSH Method "If it is a Blue State to switch to Green"; C.SetState (New GreenState ());} public void handlepull (Context C) {/ / According to the PULL method "Switch to RED"; C.SETSTATATE () (new redstate ());} public abstract void getColor () {return (color.blue)}}}
The same other status of subcategories is like Blue.
Step 2: To rewrite State Manager is the context:
Public class context {private sate = null; // We change the original Color State to the new State State; // setState is used to change the status to switch Pulic void setState (state state) { The detail portion of the switching of the PUBLIC VOID PUSH () {// State, in this case, the color change, has been encapsulated in the Handlepush of the subclass, there is no need to care about State.Handlepush (this) ; // Because SAMPLE wants to use a handover result in State, use getColor () Sample Sample = New Sample (state.getcolor ()); sample.operate ();} public void pull () {State.Handlepull (this) Sample2 Sample2 = New Sample2 (state.getcolor ()); Sample2.Operate ();}}
At this point, we also realize the State's Refactorying process.
The above is just a fairly simple instance, in practical applications, Handlepush or Handelpull processing is complex.