Java design pattern State

xiaoxiao2021-03-06  86

Design mode State Band Bridge in Man ·· BQLR State 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. It is to be described herein 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")) ") State =" Hello "; Else IF (state.euqals (" Hello ")) State =" hi "; Else IF State.euqals ("hi"))) State = "BYE"; this is "switch switching state", is to switch the status of State from "Hello" to "Hi", then switch to "Bye"; switch to " Hello, as if a rotary switch, this state change can be used to use the State mode. If there is a "Hello" -> "hi" -> "bye" -> "Hello" Direction switches, nor do not necessarily use State mode, because State mode creates a lot of subclasses, complicates, but if another behavior occurs: switch the above switching direction, or need to switch, you 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, the classic TCPConnection, TCP state has creation listening to close three, and repeatedly converts, the specific behavior that creates a listening close down is not a simple one or two sentences, suitable for use, for example: mailbox POP account, there will be Four states, start hasusername authorized quit, each state corresponds to behavior should be relatively large. Suitable for use, for example: Pick different tools in the toolbox, you can see Switch in different tools, suitable for use of State. Such as specific drawing programs The user can select different tools to draw square frame linear curves, which switches can use State.

How to use State requires two types of entities to participate: 1.State Manager Status Manager is the switch, and the context actually actually a State Manager in the state manager in the state manager. 2. Different states are inherited with different subclasses of this parent class with the parent class that achieves abstract classes or interfaces. Take the context above as an example. We want to modify it to 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 State Manager The switch behavior in State Manager is Context, there are two switching action Push push and pull pull. So in the state parent class, there must be specific processing: handlepush () handlepull (); There is also a need for a method of obtaining the PUSH or PULL results. GetColor () 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)} The same other status subclass is implemented such as Blue same. Step 2: To rewrite State Manager is the context:

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

New Post(0)