Design Patterns Study Notes (17: state mode)

Source: Internet
Author: User

1.1 Overview

allows an object to change its behavior when its internal state changes, and the object seems to modify its class. This is the definition of the state pattern.

The state of an object depends on the value of its variables, and the objects may have different states in different running environments. In many cases, the behavior effect of an object invocation method depends on its state at that time.

  For example, a thermometer ( thermometershowmessage () the information needs to be displayed according to the current temperature ( temperature based on its state. showmessage () method, which requires the showmessage () method has many conditional branching statements, for example:

 Public void ShowMessage () {    if(temperature <= -20) {        System.out.println ("Now is low" +  temperature);     }     if (Temperature >=) {        System.out.println ("Now is high temperature" +temperature);}    }

we notice thatShowMessage ()method depends on the behavior of theTemperaturethe size, which makesThermometerinstances of classes lack flexibility to respond to changes in demand and do not meet the needs of users well. For example, some users may need a thermometer when theirTemperaturevalue is greater than -show some important information; some users, when their temperatureTemperaturethe value in -to the -important information is displayed between the. Obviously in order to achieve the above requirements, the above code must be modified to meet user needs, is clearly not what people like to do.

now we need to rethink the thermometer ( THERMOMETER) class, it is found that thecode that needs to be modified in the thermometer class depends on the state of the object, which is related to the temperature, so the state of the object should be separated from the current object according to the design principle oriented to abstract and non-oriented implementation. Encapsulates the state of an object in another class. Now, design an abstract class: the Temperaturestate class, which specifies the method showtemperature ()for displaying information about temperature. The concrete class diagram looks like this:

Figure one: Encapsulating the state of an object

The thermometer class contains A number of variables declared by the Thermometerstate class, and the surface thermometer Example of a class: A thermometer class can take an instance of a subclass of any thermometer state class as its own state, and an instance of a thermometer class can delegate a state-related request to an object of the state being maintained.

The key to state mode is to encapsulate the state of an object into a separate class, and when an object invokes a method, it can delegate the state that the current object has to call the appropriate method, which is the class that the current object looks like it has modified.

Structure of the 1.2 pattern

The status mode consists of the following three roles:

(1) Environment (context): An environment is a class that contains avariable that is declared by an abstract state, whichcan refer to an instance of any specific State class. The useris interested in the behavior of an instance of the environment (context) class in some state.

(2) abstract State: An abstract state is an interface or abstract class. The abstract state definesseveral methods that are related to a particular state of the environment (context).

(3) status (concrete state): The specific state is an instance of the implementation (extended) abstract State (abstract class).

The class diagram for the state pattern structure is shown in two:

Figure II: Class diagram of the state pattern

1.3 Status advantages of the model

(1) using a class to encapsulate a state of an object, it is easy to add a new state.

(2) in the state mode,There is no need to have a large number of conditional judgment statements in the environment (context). The state of the environment (context) instance becomes clearer and easier to understand.

(3) using the status mode allows the user program to easily switch the state of the environment instance.

(4) using state mode does not cause internal state inconsistencies in instances of the environment.

(5) When a state object does not have an instance variable, each instance of the environment can share a state object.

1.4 Fit Usage Status pattern of the scene

(1) An object's behavior depends on its state, and it must change its behavior at run time based on its state.

(2) A large number of conditional branching statements are required to determine the behavior of an operation, and these conditions represent a state of an object.

Design Patterns Study Notes (17: state mode)

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.