The state mode puts the change of the state and the operation in the corresponding state together,
package com.roc.state; public class Context { private State state; public Context(State state){ this.state=state; } public State getState() { return state; } public void setState(State state) { this.state = state; } public void stateChange(){ state.action(this); } }
package com.roc.state; /** * abstract state class * @author liaowp * */ public abstract class State { public abstract void action(Context context); }
//The specific class, perform the corresponding operation, and set the state after the operation
package com.roc.state; public class OpenSate extends State{ public void action(Context context) { System.out.println("开"); context.setState(new CloseState()); } }