Process multiple states of objects and their mutual transformation-state mode (2)

Source: Internet
Author: User
2. Status mode Overview

Status ModeIt is used to resolve the state transition of complex objects in the system and encapsulation of downlink behavior of different States.. When an object in the system has multiple states, these States can be converted, and the state mode can be used for different states of the object. The State mode separates the state of an object from the object and encapsulates it into a special State class, so that the state of the object can be flexibly changed. For the client, you do not need to care about the object state transition and the current state of the object. The client can process the object in any State.

The status mode is defined as follows:

State pattern: allows an object to change its behavior when its internal state changes. The object seems to have modified its class. Its alias is the State object (Objects for States). The State mode is an object behavior mode.

Abstract state classes and specific state classes are introduced in the state mode. They are the core of the state mode, and their structure is shown in 3:

Figure 3 Status mode structure

The status mode structure includes the following roles:

● Context ):The environment class is also called the context class. It is an object with multiple States. Because of the diversity of Environment states and the behavior of objects in different States is different, the state is independent to form a separate state class. Maintain an abstract state class State instance in the Environment class. This instance defines the current state. In actual implementation, it is an object of the State subclass.

● State (abstract state class ):It is used to define an interface to encapsulate a behavior related to a specific State of the Environment class, and declare methods corresponding to different States in the abstract state class, and implement these methods in its subclass, because the behavior of objects in different States may be different, the implementation of methods in different subclasses may be different. The same methods can be written in abstract state classes.

● Concretestate (specific state class ):It is a subclass of the abstract state class. each subclass implements a state-related Behavior of the Environment class, and each specific state class corresponds to a specific state of the environment, different States have different behaviors.

In the state mode, we encapsulate the behavior of objects in different States into different state classes. In order to make the system more flexible and scalable, at the same time, we need to abstract the common behaviors under various states and introduce abstract state roles. The typical code is as follows:

Abstract class State {// declare abstract business methods. Different specific state classes can implement different public abstract void handle ();}

The service methods declared in the abstract state class are implemented in the subclass of the abstract state class, that is, the specific state classes can provide completely different implementation methods, in actual use, a status class may contain multiple business methods. If the implementations of some business methods in a specific status class are identical, you can move these methods to the abstract status class, code reuse. The typical status code is as follows:

Class concretestate extends State {public void handle () {// method specific implementation code }}

The environment class maintains a reference to the abstract state class. The setstate () method can be used to inject different State objects to the Environment class, and then call the State object method in the Environment class business methods, the typical code is as follows:

Class context {private State state; // maintain a reference to the abstract state object private int value; // other attribute values, this attribute value may change the object status. // set the State object public void setstate (State state) {This. state = State;} public void request () {// other Code state. handle (); // call the Business Method of the State object // other code }}

The environment class is actually a real state object. We just extract the state-related code from the Environment class and encapsulate it into a special State class. In the state mode structure diagram, there is a one-way association between context and state in the abstract state class. A State object is defined in context. In actual use, they may have more complex relationships, and the State and context may also have dependencies or associations.

During the use of the state mode, the state of an object can also be converted to each other. there are usually two ways to achieve State Conversion:

(1)The environment class is responsible for switching between States.At this time, the Environment class also acts as the State Manager to realize state conversion by judging certain attribute values in the Environment business methods, you can also provide a special method for Attribute judgment and state conversion, as shown in the following code snippet:

...... Public void changestate () {// determines the attribute value. Status Conversion Based on the attribute value if (value = 0) {This. setstate (New concretestatea ();} else if (value = 1) {This. setstate (New concretestateb ());}......} ......

(2)The specific status class is responsible for switching between States.You can determine certain attribute values of the Environment class in the Business Method of the specific state class, and then set a new State object for the Environment Class Based on the situation to realize state conversion. Similarly, you can also provide a dedicated Method to Determine the attribute values and convert the status. In this case, there will be dependencies or associations between the status class and the Environment class, because the status class needs to access the attribute values in the Environment class, as shown in the following code snippet:

...... Public void changestate (context CTX) {// status conversion if (CTX. getvalue () = 1) {CTX. setstate (New concretestateb ();} else if (CTX. getvalue () = 2) {CTX. setstate (New concretestatec ());}......} ......

 

Thoughts

What are the differences between the two state conversion methods?

[Author: Liu Wei http://blog.csdn.net/lovelion]

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.