The State mode can be used in the following two cases:
1. The behavior of an object depends on its state, and it must change its behavior according to its state at runtime.
2. An operation contains a large multi-branch conditional statement, and these branches depend on the state of the object. This state is usually represented by one or more enumerated constants. Generally, multiple operations contain the same condition structure. In State mode, each condition branch is placed in an independent class. This requires you to take the object state as an object based on the object's own situation. This object can be independent of other objects.
For better understanding, let's give an example:
A drawing program has a control panel that lists various drawing tools, their choice of colors, droppers, and other tools. When a user selects or changes a drawing tool, you can use different tools for drawing or other operations.
We first use a general method (non-state mode) to simulate the process of selecting a paint brush tool:
// Select the event handling method as the paint brush tool
Public void onselectpen (INT state ){
// Create different paint brush tools based on different options and draw pictures
If (State = circle_pen ){
Circlepen CP = new circlepen (); // create a circular brush
CP. drawcircle (paramters); // draw a circle
} Else if (State = square_pen ){
Squarepen sp = new squarepen (); // create a square paint brush
Sp. drawsquare (paramters); // draw a square
} Else if (State = line_pen ){
Linepen Lp = new linepen ();
LP. drawline (paramters );
}
... // Other paint brushes
You can see that you need to add a judgment condition for each paint brush tool. When the paint brush tool increases or decreases a lot, the code has to be modified a lot. State mode abstracts various state behaviors, creates a subclass of the state class for each possible state, and manages the current state of the State subclass object through a context class, encapsulate the call of the Status subclass. You can change the behavior of different states by changing the state managed by the context object.
The Code implemented in the status mode is as follows:
Import java. util .*;
// Graphic tool interface
Interface State {
Public void draw ();
}
// Draw line tool
Class line implements State {
@ Override
Public void draw (){
// Todo auto-generated method stub
System. Out. println ("draw a straight line ");
}
}
// Circle Tool
Class circle implements State {
@ Override
Public void draw (){
// Todo auto-generated method stub
System. Out. println ("draw a circle ");
}
}
// Eraser Tool
Class empty implements State {
@ Override
Public void draw (){
// Todo auto-generated method stub
System. Out. println ("erase canvas ");
}
}
// Configure the status of each tool
Class context {
// Maintain the list of various tools
Map <string, State> toolbar = new hashmap <string, State> ();
State toolstate;
Public void addstate (string name, state ){
Toolbar. Put (name, State );
}
// Change the status
Public void selectstate (string name ){
Toolstate = toolbar. Get (name );
}
// Perform specific actions
Public void action (){
Toolstate. Draw ();
}
}
// Test class
Public class client {
Public static void main (string [] ARGs ){
Context tool = new context ();
Tool. addstate ("line", new line ());
Tool. addstate ("circle", new circle ());
Tool. addstate ("empty", new empty ());
Tool. selectstate ("line ");
Tool. Action ();
Tool. selectstate ("empty ");
Tool. Action ();
Tool. selectstate ("circle ");
Tool. Action ();
}
}
Output:
Draw a straight line
Erase canvas
Draw a circle
Advantage: 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.
Disadvantage: Some systems may have too many specific status classes.
Reprinted from http://hi.baidu.com/%D0%D0%D7%DF%D4%DA%BF%D5%D6%D0/blog/item/8d5b31f9536f7153252df2c1.html