Status pattern of Java Design Pattern
When to use the State mode is usually used in actual use, so it is suitable for "status" switching. Because we often use If else if else for status switching, If the status is determined to appear repeatedly, we need to think about whether the State mode can be used. The difference between the switch status and the general status judgment is described here. The general status judgment also has the if... elseif 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. The difference in the state value is determined by the which variable. The 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 the switch status. It switches the state from hello to hi, and then to bye. Switching to hello is like a rotating switch, the State mode can be used for this change. If you simply switch from hello --> hi --> bye --> hello, you do not need to use the State mode because the State mode creates many subclasses, which is complicated, however, if another behavior occurs: Switch the preceding switch direction in turn, or you need to switch it at will, the State is required.
Below is a general state switch
Public class Context {private Color state = null; public void push () {// if the current red status, switch to blue if (state = Color. red) state = Color. blue; // switch to green else if (state = Color. blue) state = Color. green; // if the current black status is black, switch to red else if (state = Color. black) state = Color. red; // switch to black else if (state = Color. green) state = Color. black; Sample sample = new Sample (state); sample. operate ();} public void pull () {// the opposite of push status switching if (state = Color. green) state = Color. blue; else if (state = Color. black) state = Color. green; else if (state = Color. blue) state = Color. red; else if (state = Color. red) state = Color. black; Sample2 sample2 = new Sample2 (state); sample2.operate ();}}
Two types of entities are required to override the State in policy mode: 1. the state manager is a switch. In the preceding example, the Context is actually a state manager, and there is a switching action for the state manager. 2. for a parent class implemented using an abstract class or interface, different States inherit different subclasses of the parent class.
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 methods in the parent class correspond to the switch behavior in the state manager. In this example, there are two switch actions in the Context, push and pull. then, in the state parent class, you need to take two actions: handlepush () handlepull (); at the same time, you need a method getcolor () to obtain the push or pull results ()
The following lists the implementation status switching sequence of specific subclass push: blue --> green --> black --> red --> bluepull: blue --> red --> black --> green --> blue
Public class BlueState extends State {public void handlepush (Context c) {System. out. println ("turns green); c. setState (new GreenState ();} public void handelizull (Context c) {System. out. println ("turns to red); c. setState (new RedState ();} public abstract void getcolor () {return (Color. blue );}}
Public class GreenState extends State {public void handush ush (Context c) {System. out. println ("turns black); c. setState (new BlackState ();} public void handequalull (Context c) {System. out. println ("turns blue); c. setState (new BlueState ();} public abstract void getcolor () {return (Color. green );}}
Public class BlackState extends State {public void handush ush (Context c) {System. out. println ("turns to red); c. setState (new RedState ();} public void handequalull (Context c) {System. out. println ("turns to red); c. setState (new RedState ();} public abstract void getcolor () {return (Color. black );}}
Public class RedState extends State {public void handush ush (Context c) {System. out. println ("turns blue); c. setState (new BlueState ();} public void handpolicull (Context c) {System. out. println ("turns black); c. setState (new BlackState ();} public abstract void getcolor () {return (Color. red );}}
Rewrite the State manager again, that is, the Context in this example.
Public class Context {private Sate state = null; // we changed the original Color state to the new State state; // setState is used to change the state. Use setState to implement State switching. pulic void setState (state State) {this. state = state;} public void push () {// details of the status switch. In this example, the color changes have been encapsulated in the handush ush of the subclass, state is not required here. handlepush (this); // assume that the sample uses a switchover result in the state, and getColor () Sample sample = new Sample (state. getColor (); sample. operate ();} public void pull () {state. handequalull (this); // assume that the sample uses a switching result in the state and uses getColor () Sample2 sample2 = new Sample2 (state. getColor (); sample2.operate ();}}
The State mode is widely used in workflows, games, and other systems, and even the core function design of these systems. For example, in an ERP system, there are multiple States of a letter of approval: not handled; in process; being instructed; under review; completed and other statuses. The state machine can encapsulate the change rules of this state, so that users of this State do not need to be involved when the status is expanded.
Before using the state mode, the client needs to change the state, and the implementation of the state change is trivial or complex. After the State mode is used, the client can directly use Event to implement the Event. You do not have to worry about how the Event changes. These are implemented internally by the state machine and so on. This is an Event-condition-State. The State mode encapsulates the condition-State section. Each State forms a subclass, and each State only cares about its next possible state, thus forming a state conversion rule. If a new status is added, only modification and definition of the previous status are involved.
The main advantage of the State mode is that it encapsulates the conversion rules and enumerates possible states. It puts all actions related to a State into a class and can conveniently add new States, you only need to change the object State to change the object behavior. You can also share multiple environment objects with one State object to reduce the number of objects in the system; its disadvantage is that using the state mode will increase the number of System Classes and objects, and the structure and implementation of the state mode are complicated. Improper use will lead to confusion of program structure and code, the status mode that can be switched does not meet the requirements of the "Open and Close principle.