Design mode 19 - state mode

State mode
When an object's internal state changes, it is allowed to change its behavior. The object looks like it has changed its class.
The core is encapsulation. The change of state causes the change of behavior, which looks like the change of the corresponding class of the object from the outside.
Advantage:
1. Clear structure; 2. Follow the design principle, which well reflects the principle of opening and closing and the principle of single responsibility;
3. Very good encapsulation.
Disadvantages:
Each state is a subclass, and there will be too many subclasses -- class inflation.
Usage scenario:
1. The scene where the behavior changes with the change of state; 2. The substitute of condition and branch judgment statement.
Note: it is applicable to when an object changes its state, its behavior changes greatly with it, that is to say, when the behavior is constrained by the state, it can
Use state mode, and it is better not to use more than 5 objects.

//Abstract state: responsible for the definition of object state, and encapsulate the role of environment to achieve state switching.
public abstract class State {
	protected Context context;
	public void setContext(Context context) {
		this.context = context;
	}
	public abstract void handle1();
	public abstract void handle2();
}
//Environment role: define the interface required by the client, and be responsible for the specific state switching.
/**
 * Two unwritten constraints:
 * 1,State objects are declared as static constants, and several state objects are declared as static constants.
 * 2,The environment role has all the behaviors defined by the state Abstract role, and the specific execution uses the delegation method.
 *
 */

public class Context {
	public final static State STATE1=new ConcreteState1();
	public final static State STATE2=new ConcreteState2();
	//current state
	private State currentState;
	public State getCurrentState() {
		return currentState;
	}
	public void setCurrentState(State currentState) {
		this.currentState = currentState;
		this.currentState.setContext(this);
	}
	//Behavior delegation
	public void handle1(){
		this.currentState.handle1();
	}
	public void handle2(){
		this.currentState.handle2();
	}
	
}
//Specific status role: each specific status must fulfill two responsibilities: behavior management of this status and trend status processing.
//Generally speaking, it is what this state should do and how to transition from this state to other states.
public class ConcreteState1 extends State {
	@Override
	public void handle1() {
		
	}
	@Override
	public void handle2() {
		//Set the current state to state2
		super.context.setCurrentState(Context.STATE2);
		//Transition to state2 state, implemented by Context
		super.context.handle2();
	}
}
public class ConcreteState2 extends State {
	@Override
	public void handle1() {
		//Set the current state to state2
		super.context.setCurrentState(Context.STATE2);
		//Transition to state2 state, implemented by Context
		super.context.handle2();
	}
	@Override
	public void handle2() {
		
	}
}


 

Posted by Jamesm on Wed, 27 Nov 2019 10:21:17 -0800