Java and mode Learning Series-state mode

Source: Internet
Author: User

 

I, State structure

The State mode allows an object to change its behavior when its internal state changes. The state pattern encapsulates the behavior of the studied object in different State objects. Each State object belongs to a subclass of an abstract state class. The intention of the state mode is to change the behavior of an object when its internal state changes.

Shows the structure:

By using the polymorphism principle, You can dynamically change the State content of the context attribute of the Environment class, so that it changes from pointing to a specific State class to pointing to another specific state class, in this way, sampleoperation () is executed by different specific status classes. The Code is as follows:

// Context class

Public class context {

Private State
State;

Public void
Sampleoperation (){

State. sampleoperation ();

}

// There must be a method to change the State object

Public void setstate (State state ){

This. State = State;

}

}

// State interface

Public interface state {

Void
Sampleoperation ();

}

// Concretestate class

Public class concretestate implement State {

Public void
Sampleoperation (){}

}

Effect of status mode:

(1) The State mode creates a subclass of the state class for each possible State obtained by the system. When the system status changes, the system changes the selected subclass. All actions related to a specific State are encapsulated into a specific object, so that the behavior definition is localized. For the same reason, if a new State and its corresponding behavior need to be defined, you can easily return to the system by setting up a new subclass, you do not need to change other child classes.

(2) because every state is packaged into the class, you do not need to use a procedural method to transfer the name using lengthy conditions.

(3) use the state mode to make the system status change very obvious. Because some attributes (internal variables) are not used to indicate the status of the system, you do not have to worry about errors caused by improper modification of these attributes.

(4) You can use the same state class objects in different parts of the system. The shared object method is consistent with the metadata mode. In fact, at this time, these State objects are basically the metadata mode with only behavior but no internal state.

(5) the disadvantage of the state mode is that it will cause a large number of small state classes: the advantage is that the program is exempt from a large number of conditional transfer statements, so that the program is actually easier to maintain.

(6) The State subclass selected by the system is inherited from an abstract state class or interface. The features of Java make it safer to use the state pattern in Java. Polymorphism is the core of the state pattern.

II, Under what conditions do I use the status mode?

(1) the behavior of an object depends on its state. The behavior of an object must change with its State.

(2) A method of an object depends on one or more conditional transfer statements, a large amount of code. The State mode encapsulates each branch of the conditional transfer statement into a separate class. This allows these conditional transfer branches to exist and evolve independently in the form of classes.

III, Status mode and Policy Mode

The status mode is often confused with the policy mode. Their simple class diagrams are as follows:

An easy way is to check whether the environment role has a clear transition between the state and the state. If the environment role has only one status, the policy mode should be used. The rule Mode features that once an environment role selects a specific policy class, it will not change the specific policy class throughout the life cycle of the Environment class. Another subtle difference is that the environmental class of the Policy mode chooses a specific strategy class, while the environmental class of the state mode is put into a specific State by external reasons. The policy selected in the Policy mode often does not clearly tell the client the specific policy it chooses. The status mode is the opposite. In the status mode, the status of the environment role clearly tells the client.

 

 

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.