Design Mode Study Notes (15th)-state mode

Source: Internet
Author: User
Tags case statement

I. State mode definition:
Allows an object to change its behavior when its status changes. It seems that the object has modified its class.
Ii. Mode explanation
The State mode mainly solves the problem of different processing operations that are often encountered during development according to different States, most people use the switch-case statement for processing, which causes a problem: too many branches, and if a new State is added, the original code needs to be compiled. The State mode encapsulates these different States to handle such problems. When the state changes, the state is processed and then switched to another State, that is to say, the status switching responsibility is handed over to the specific status class for responsibility. at the same time, there are many similarities between the State mode and the Strategy Mode. It must be noted that the two ideas are the same, but the encapsulated things are different: the State mode encapsulates different states, the stategy mode encapsulates different algorithms.

Iii. Structure Diagram
The structure of the State mode is as follows:

4. How to use it?
1) define a state interface, which has a unified method to encapsulate the behavior corresponding to a specific State.
2) define different status classes concretesate to implement the state interface.
3) Each status class implements the action corresponding to a state in the environment (context.
4) define a State Manager context.
V. Example
Interface State {
Public void handle (context CTX );
}
Class concretestatea implements State {

Public void handle (context CTX ){
System. Out. println ("handle by concretestatea ");
If (CTX! = NULL ){
CTX. changestate (New concretestateb ());
}

}

}
Class concretestateb implements State {

Public void handle (context CTX ){
System. Out. println ("handle by concretestateb ");
If (CTX! = NULL ){
CTX. changestate (New concretestatea ());
}

}

}
Class context {
Private State _ state;
Public context (State state ){
_ State = State;
}

Public void request (){
If (_ state! = NULL ){
_ State. Handle (this );
}
}
Public void changestate (State S ){
If (_ state! = NULL ){
_ State = NULL;
}
_ State = s;
}
}
Public class stateclient {

Public static void main (string [] ARGs ){
State state = new concretestatea ();
Context context = new context (State );
Context. Request ();
Context. Request ();
Context. Request ();
Context. Request ();

}

}
Output result:
Handle By concretestatea
Handle By concretestateb
Handle By concretestatea
Handle By concretestateb

Each request changes the status once, and the corresponding action is executed.

Vi. Applicability
1) the behavior of an object depends on its state, and it must change its behavior according to its state at runtime.
2) an object contains a large number of condition branch statements, which depend on its State. This state is usually represented by one or more enumerated constants. Multiple operations usually contain the same structure. In State mode, each branch is put into an independent class. This allows you to take the object state as an object based on the object's own situation. This object can be independent of other objects.
VII. Advantages and Disadvantages
1) Advantages: This avoids the large if or case statements generated to determine the status. After the object behavior is handed over to the state class for maintenance, the upper-layer program only needs to maintain the conversion rules between States.
2) some systems may have too many specific status classes.
VIII. References

Http://www.cppblog.com/converse/archive/2006/08/07/10902.html

Http://www.blogjava.net/flying/archive/2006/08/29/66472.html

 

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.