State of design pattern

xiaoxiao2021-03-06  74

State mode definition: Different status, different behavior; or say that each state has a corresponding behavior.

When is it used? State mode is more in actual use, suitable for "status switch". Because we often use if elseif else to switch, if this judgment switch repeatedly, we will be able to take it. State mode.

Not just according to the status, there is also a property. If the properties of an object are different, the behavior of the object is different, this is relatively high in the database system, we often in the end of a data table, plus the Property property meaning The field is used to identify records recorded in some special nature in the record, and the change in this property (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 determined according to the Which variable. Which and state have no relationship. 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 do one of the above to switch "Hello" -> "Hi" -> "BYE" -> "Hello", it is not necessarily necessary to use a STATE mode, because the State mode creates a lot of subclasses. Complexity, but if another behavior occurs: switches 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, you can see how to switch in different tools, suitable for use in specific plot programs, users can select different tools to draw square frame straight lines, this state switching can use State.

How to use State requires two types of entities:

1.State Manager Status Manager is the switch. The Context actually is actually a State Manager in the STATE Manager. 2. Different status is inherited with the parent class that implements an abstract class or interface, different states Different subclasses of this parent class.

Take the context as an example. We want to modify it, build two types of entities. Step 1: First create a parent class:

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 class should correspond to the switch behavior in State Manager. In the state manager, this example is Context, there are two switching action Push push and pull. So in the state parent class, there must be specific processing of these two actions. : Handlepush () handlepull (); also requires a method of getting a PUSH or PULL result getColor () below is the implementation of specific subclasses:

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)}}}

Subclasses in other states implementations such as 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) { This.State = state;} Public void push () {// Switching details of the switch, in this example, the color change, has been packaged 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.

Status mode advantages: (1) The package conversion process is the possible state of the conversion rule (2) enumeration, so it is necessary to determine the state type in advance.

The status mode can allow the client to change the conversion behavior of the state, and the status machine can automatically change the state, the state machine is a relatively independent and complex mechanism, and the specific reference can be referred to as a state machine open source project: http: //sourceforge.net / Projects / SMFramework /

Status mode has a large number of applications in various systems such as workflow or games, even in the core functional design of these systems, such as government OA, a number of states in a batch: nothing; is being processed; is insisting; All states have been completed, using the state machine can package this state change rule, thereby reaching the expansion state, does not have to be involved in the state.

In online games, a game activity is started; playing; playing; losing wins, etc. The entire game architecture function is implemented to determine the dominant role. Status mode substance: Before using status mode, the client needs to intervene in changing state, and the implementation of the status change is trivial or complex.

After using the status mode, the outside of the client can use the event EVENT implementation directly. It does not have to care about how the event causes the state change, which is implemented inside the state machine.

This is an Event-Condition-State, and the status mode encapsulates the Condition-State section.

Each state forms a subclass, each state only cares about its next possible state, so that the rule of state transition is formed in invisible. If the new state is added, only the previous state modifications and definitions are involved.

There are several ways to implement: one implement next () in each state, specifying the next state; there is a method, set a stateEnter, set the stateEnter state entry and STATEEXIT status to exit behavior.

The state will illustrate the process from one aspect, and the process changes over time, and the status is a time piece for intercepting the flow.

related articles:

Summary State mode use experience from workflow state machine practice

Reference Resources: The State and Stategyhow TO IMPLEMENT State-Dependent Behavior The State Patterns

转载请注明原文地址:https://www.9cbs.com/read-93054.html

New Post(0)