5. Use the environment class to implement status conversion
When status switching is implemented in status mode,A specific state class can call the setstate () method of the environment context to convert the State, or the environment context can be used to convert the state.. In this case, you may need to modify the source code of another specific State or environment class to add a new state class. Otherwise, the system cannot switch to the new State. However, for the client, you do not need to care about the status class. You can set the default status class for the Environment class, and hand over the status switching work to the specific status class or environment class to complete, the specific conversion details are transparent to the client.
In the above "bank account status transition" instance, we use a specific status class to convert the status. Each specific status class contains a statecheck () method, this method implements State conversion. In addition, we can use the environment class to implement state conversion,Environment, as a State Manager, implements a unified conversion between various States.
The following usesLoop statusTo illustrate how to use the environment class to achieve State Conversion:
A developer of Sunny software company wants to develop a screen magnifier tool. Its specific functions are described as follows: After you click the "magnifier" button, the screen will be doubled. Click the "magnifier" button again to double the screen. After you click the button for the third time, the screen will be restored to the default size. |
We can consider using the status mode to design the screen magnifier tool. We define three screen states, including normalstate, largerstate, and largeststate, to correspond to the three screen states, the screen type is normal, double, and quadruple, respectively. The screen type acts as the Environment class, and its structure is shown in 6:
Figure 6 screen magnifier Tool Structure
The core code of this instance is as follows:
// Screen class screen {// enumerate all States. currentstate indicates the current State private State currentstate, normalstate, largerstate, largeststate; Public screen () {This. normalstate = new normalstate (); // create a normal state object this. largerstate = new largerstate (); // create a 2x magnified State object this. largeststate = new largeststate (); // create a four-fold magnified State object this. currentstate = normalstate; // set the initial state this. currentstate. display ();} public void setstate (State state) {This. curr Entstate = State;} // click the event processing method, and the call to the Business Method in the status class and the transition to the state class public void onclick () {If (this. currentstate = normalstate) {This. setstate (largerstate); this. currentstate. display ();} else if (this. currentstate = largerstate) {This. setstate (largeststate); this. currentstate. display ();} else if (this. currentstate = largeststate) {This. setstate (normalstate); this. currentstate. display () ;}}// abstract state class Abstract Class state {public abstract void display () ;}// normal state class normalstate extends State {public void display () {system. Out. println ("normal size! ") ;}} // Status class largerstate extends State {public void display () {system. Out. println (" 2x size! ") ;}// Four times the status class largeststate extends State {public void display () {system. Out. println (" four times the size! ");}}
In the above Code, all state conversion operations are implemented by the Environment Class Screen. At this time, the Environment class acts as the State Manager role. If you need to add a new State, such as the "", You need to modify the environment class, which violates the "Open and Close principle" to some extent, but has no impact on other status classes.
Write the following client code for testing:
class Client {public static void main(String args[]) {Screen screen = new Screen();screen.onClick();screen.onClick();screen.onClick();}}
The output result is as follows:
Normal size! Twice the size! Quadruple size! Normal size! |
[Author: Liu Wei http://blog.csdn.net/lovelion]