The 21st chapter of the design pattern-state mode (Java implementation)
"What is it you do?" Why are you so depressed? "Last night was a new tonic, but also a small cage bag, sleep a little late ah." The words of the Eagle brother is very good, every day to find Shedan, and sparring martial arts, think not invincible are difficult ah, there is the Blablabla "(the author has been dragged away). Cough, today the state of the brother to rush over, the following shining debut.
Self introduction of State mode
Today is not in the state, may be due to the cause of the poor universe, OK, first of all, the definition: Allow an object to alter its behavior when it internal state changes. The object will appear changes its class. That is, when an object is internally altered to allow it to alter its behavior, it looks like it has changed its classes. My core is "encapsulation", yes, not the legendary "Silver scale chest armor" blue outfit. Changes in the state can cause changes in behavior, from the outside it seems like the object corresponding to the class has changed generally, Buddy's general class diagram is as follows:
Fish brother is not in my more than a little bit: context will be related to the state of the request to the current Concretestate object processing, then, the context can pass itself as a parameter to the state object processing the request, so that the state object can access the context when necessary, The context is the primary interface that the customer uses, and the client can configure a context with the state object, and once a context is configured, its customers no longer need to deal directly with the state object. The context or Concretestate subclass can determine which state is the successor of the other and under what conditions the state is converted.
Self-analysis of State mode
First introduce the following advantages:
- The localization of behaviors related to a particular state and the separation of behaviors that complement different states is a good manifestation of the principle of openness and the principle of single responsibility. Adding state and modifying state becomes easy.
- It avoids the complexity of the program and improves the maintainability of the system. Make the structure clear.
- The state object can be shared.
Gold without can't pure, no perfect, I naturally can not exception, the disadvantage is actually one:
- If there is too much state, the subclass expands.
Implementation of State mode
Today we will be the author of the activities of the great last night to show how to achieve, first we implement the context of the class, this is very important, the code is as follows:
1 Public classcontext{2 //Define State3 Public Final Staticstate STATE1 =NewConcreteState1 ();4 Public Final Staticstate STATE2 =NewConcreteState2 ();5 6 //Current status7 PrivateState CurrentState;8 9 Ten //Get method gets the current state One PublicState Getcurrentstate () { A returnCurrentState; - } - //Set method sets the current state the PublicState Setcurrentstate (state currentstate) { - This. CurrentState =CurrentState; - - //Toggle Status + This. Currentstate.setcontext ( This); - } + A //Behavior Delegation at Public voidHandle1 () { - This. Currentstate.handle1 (); - } - - Public voidHandle2 () { - This. Currentstate.handle2 (); in } - to}View Code
Well, this context class defines the interface that the client needs and is also responsible for the switching of the state, that is, the work is done by him, and the next is the abstract class of state, which defines an interface that allows subclasses to access, as well as abstract behavior:
1 Public Abstract classstate{2 //Define a context role that provides subclass access3 protectedcontext context;4 //set up a context role5 Public voidSetContext (Context context) {6 This. Context =context;7 }8 9 //Behavior 1Ten Public Abstract voidhandle1 (); One //Behavior 2 A Public Abstract voidhandle2 (); -}
Then is the first state of the concrete implementation of the code, inherited from the abstract class:
1 Public classConcreteState1extendsstate{2 @Override3 Public voidhandle1{4System.out.println ("Look at Xiao Bao Bao");5 }6 7 @Override8 Public voidhandle2{9 //set the current state to State2Ten Super. Context.setcurrentstate (context.state2); One //transition to State2, implemented by context A Super. Context.handle2 (); - } - the}View Code
Finally, the second state-specific implementation class, is to see the new fan:
1 Public classConcreteState1extendsstate{2 3 4 @Override5 Public voidhandle1{6 //set the current state to State17 Super. Context.setcurrentstate (context.state1);8 //transition to State2, implemented by context9 Super. Context.handle1 ();Ten } One A @Override - Public voidhandle2{ -System.out.println ("Look at the new fan"); the } - - - +}View Code
Well, the author is in the two states of the infinite switch, and then, then can't extricate themselves, then the spirit of depression, and then there is no then ~
Application scenario of State mode
The main application scenario is two, look what I do. I am not the only one, scene two, wrong, scene is not two, is the scene has two:
- When an object's behavior depends on its state, and it must change its behavior at run time depending on the state.
- When there are many branch statements and conditional statements in an operation, and these spoke/conditional statements depend on the state of the object, it is my turn to come.
Above. (Something: It is said that the author closed the Black House for a week because of the state pattern of the black author.) )
PS: This blog welcome forwarding, but please specify the blog address and author ~
Blog Address: http://www.cnblogs.com/voidy/
Blog: http://voidy.net
<. ))) ≦
The 21st chapter of the design pattern-state mode (Java implementation)