concept
The behavior of a class changes based on its state
The main idea is to abstract all kinds of concrete state classes. That is, there will be many state classes.
Usage scenarios
- Code contains a large number of conditional statements related to object state
- Behavior changes with state
Characteristic
- States can be transformed from one another, such as workflow.
- Each state can complete its own task independently.
Code
Code scenario
At present, there are three statuses for imitating workflow: AState, BState and CState. The flow process of the statuses is AState - > BState - > CState
Specific code
State abstract class AbstractState
/** * State abstract class * @author GaoYuan * @date 2018/11/11 8:42 a.m. */ public abstract class AbstractState { /** Encapsulates the context */ protected Context context; public void setContext(Context context){ this.context = context; } /** Execution method */ abstract void run(); /** Switch to next state */ abstract void next(); }
Specific status class
public class AState extends AbstractState { /** Execution method */ @Override public void run() { System.out.println("implement AState Of run() Method"); } /** Switch to next state */ @Override void next() { System.out.println("implement AState Of next() Method"); // The next state defined is BState context.setState(new BState()); } } public class BState extends AbstractState { /** Execution method */ @Override public void run() { System.out.println("implement BState Of run() Method"); } /** Switch to next state */ @Override void next() { System.out.println("implement BState Of next() Method"); // The next state defined is CState context.setState(new CState()); } } public class CState extends AbstractState { /** Execution method */ @Override public void run() { System.out.println("implement CState Of run() Method"); } /** Switch to next state */ @Override void next() { System.out.println("implement CState Of next() Method"); System.out.println("It's the last state!"); } }
Context
/** * context * @author GaoYuan * @date 2018/11/11 8:16 a.m. */ public class Context { private AbstractState state; public AbstractState getState() { return state; } /** Set current state */ public void setState(AbstractState state) { this.state = state; // Remember setContext, or null pointer this.state.setContext(this); } /** Execution method */ public void run(){ this.state.run(); } /** Next state */ public void next(){ this.state.next(); } }
Test class
/** * The flow process of contract status is as state - > bstate - > cstate * @author GaoYuan * @date 2018/11/11 8:45 a.m. */ public class TestMe { public static void main(String[] args){ Context context = new Context(); context.setState(new AState()); context.run(); System.out.println("Current status:" + context.getState().toString()); context.next(); System.out.println("Current status:" + context.getState().toString()); context.run(); System.out.println("Current status:" + context.getState().toString()); context.next(); System.out.println("Current status:" + context.getState().toString()); context.run(); System.out.println("Current status:" + context.getState().toString()); context.next(); System.out.println("Current status:" + context.getState().toString()); } }
output
Execute the run() method of AState Current status: com.foruo.learn.designmode.state.AState@2503dbd3 Execute the next() method of AState Current status: com.foruo.learn.designmode.state.BState@4b67cf4d Execute the run() method of BState Current status: com.foruo.learn.designmode.state.BState@4b67cf4d Execute the next() method of BState Current status: com.foruo.learn.designmode.state.CState@7ea987ac Execute the run() method of CState Current status: com.foruo.learn.designmode.state.CState@7ea987ac Execute the next() method of CState It's the last state! Current status: com.foruo.learn.designmode.state.CState@7ea987ac
Code cloud
Blog
https://my.oschina.net/gmarshal/blog/2875911
Welcome to my personal wechat subscription number: (it is said that this head portrait app is dedicated to apes)