Java State mode)

Source: Internet
Author: User

Java State mode)
State: different states and actions; or, each State has a corresponding action. When to use the State mode, the State mode is usually used in actual use. It is suitable for "status switching ". Because we often use If elseif else for status switching, If the status is determined to appear repeatedly, we need to think about whether the State mode can be used. It is not only based on status, but also attributes. If the attributes of an object are different, the behavior of the object is different. This is frequently used in the database system. We often add fields with the property attribute meanings at the end of a data table, it is used to identify records of some special properties. Changes (switching) of this attribute may occur at any time, and State may be used. In actual use, there are many status switches similar to the switch, but sometimes they are not so obvious, depending on your experience and depth of understanding of the system. The difference between "switch status" and "general state judgment" is described here. "General State judgment" also has if .. elseif structure, such as: 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 state value is determined by the which variable. which and state are irrelevant. If changed to: if (state. euqals ("bye") state = "hello"; else if (state. euqals ("hello") state = "hi"; else if (state. euqals ("hi") state = "bye"; this is "switch status", which is to switch the state from "hello" to "hi ", switch to "bye"; switch to "hello", as if a rotation switch, this State change can use the State mode. If you simply switch "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 previous switch direction in turn, or if you need to switch freely, State is required. See the following example: 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; // switch to green else if (state = Color. blue) state = Color. green; // 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 ();} pu Blic void pull () {// the opposite of the push status switch 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 () ;}in the preceding example, we have two actions: push and pull, changed the Context color. Now, we need to use the State mode to optimize it. In addition, note: In the above example, the state change is just a simple color assignment. The specific behavior is very simple, and the State is suitable for a large number of specific behaviors. Therefore, in this example, in actual use, you do not have to use the State mode. This will increase the number of sub-classes and simplify the process. For example, a bank account is often converted between the Open and Close statuses. For example, for a classic TcpConnection, the Tcp State can be created by creating three listeners and switching them over and over again. The specific action of creating a listener is not completed in one or two simple sentences, and the State is suitable for use. For example, the POP account in the mailbox has four statuses: start HaveUsername Authorized quit. The action corresponding to each status should be large and suitable for the State. For example, selecting different tools in the toolbox can be seen as switching between different tools, which is suitable for using State. For a specific drawing program, you can select different tools to draw a box line curve. In this case, you can use State to switch the status. Two types of entities are required to use the State mode state: state manager State manager, which is a switch. For example, the Context in the preceding example is actually a state manager, there is a switching action for the status in the state manager. For a parent class implemented using an abstract class or interface, different States inherit different subclasses of the parent class. The preceding Context is used as an example to modify it and create two types of entities. Step 1: first create a parent class: public abstract class State {public abstract void handush ush (Context c); public abstract void handlepull (Context c); public abstract void getcolor ();} the methods in the parent class correspond to the switch action in the state manager. In the state manager, in this example, there are two switch actions in Context: push and pull. in the parent class of the state, 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 implementation of specific sub-classes is as follows: public class BlueState extends State {public void handush ush (Context c) {// according to the push method, "switch to green if blue is used"; c. setState (new GreenState ();} public void handequalull (Context c) {// according to the pull method, "switch to red in the case of blue"; c. setState (new RedState ();} public abstract void getcolor () {return (Color. blue)} Similarly, subclasses of other States are implemented like blue. Step 2: rewrite the State manager again, that is, the Context: p ublic class Context {private Sate state = null; // We changed the original Color state to the new State state. // setState is used to change the state State. Use setState to switch the state to pulic void setState (state 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); // because the sample uses a switching result in the state, use getColor () Sample sample = new Sample (state. getColor ()); Sample. operate ();} public void pull () {state. handequalull (this); Sample2 sample2 = new Sample2 (state. getColor (); sample2.operate ();} so far, we have implemented the State refactorying process. The above is just a simple example. In actual applications, handhandush ush or handelpull processing is complicated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.