Preface
Blogger github
Blogger's personal blog http://blog.healerjean.com
1. Interpretation
1. The behavior of an object depends on its state, and it must change its behavior at runtime according to the state.
2. The code contains a large number of conditional statements related to the state of the object: an operation contains a large number of conditional statements (if else or switch case), and these conditions depend on the state of the object. This state is usually represented by one or more enumeration constants.
//Running action @Override public void run() { switch (state) { case OPEN_STATE: System.out.println("Open the door: Do nothing"); break; case CLOSE_STATE: System.out.println("Closed: Operating..."); setState(RUN_STATE); break; case RUN_STATE: System.out.println("Running state: doing nothing"); break; case STOP_STATE: System.out.println("Stop: Running..."); setState(RUN_STATE); } }
Usually, there are multiple operations that contain the same conditional structure. The State pattern places each conditional branch into a separate class. This allows you to treat the state of an object as an object, depending on the object itself, which can change independently of other objects.
1.1. State abstract class
public abstract class LiftState { /** * Define an environmental role, which is the functional change caused by the change of encapsulation state */ protected Context context; public void setContext(Context context) { this.context = context; } /** * Elevator door open */ public abstract void open(); /** * Elevator door closed */ public abstract void close(); /** * The elevator runs up */ public abstract void run(); /** * The elevator has to stop. */ public abstract void stop(); }
1.2. Elevator Opening Subclass
public class OpenningState extends LiftState { @Override public void open() { System.out.println("Elevator door open..."); } @Override public void close() { System.out.println("The elevator is ready to go from Openning State to Closeing state"); super.context.setLiftState(Context.closeingState); super.context.getLiftState().close(); } @Override public void run() { throw new RuntimeException("The elevator can not run when the door is open, which is prone to danger."); } @Override public void stop() { throw new RuntimeException("The elevator stops when the door opens, so it doesn't need to be operated."); } }
1.3. Elevator Closing
package com.hlj.moudle.design.D08 Management status.D19Status State mode; /** * @author HealerJean * @ClassName ClosingState * @date 2019-08-20 21:29. * @Description What can the elevator do after the door closes? */ public class ClosingState extends LiftState { @Override public void open() { System.out.println("The elevator is ready to go from Closing State to Opening state"); super.context.setLiftState(Context.openningState); super.context.getLiftState().open(); } /** * The elevator door is closed. This is the action to be done when the elevator door is closed. */ @Override public void close() { System.out.println("Elevator door closed..."); } @Override public void run() { System.out.println("The elevator is ready to go from Closing State to Runing state"); super.context.setLiftState(Context.runningState); super.context.getLiftState().run(); } @Override public void stop() { System.out.println("The elevator is ready to go from Closing State to Stoping state"); super.context.setLiftState(Context.stoppingState); super.context.getLiftState().stop(); } }
1.4. Elevator RunningState subclass
package com.hlj.moudle.design.D08 Management status.D19Status State mode; /** * @author HealerJean * @ClassName RunningState * @date 2019-08-20 21:30. * @Description What actions can elevators do in operation */ public class RunningState extends LiftState { @Override public void open() { throw new RuntimeException("Elevator door can not be opened in operation"); } @Override public void close() { throw new RuntimeException("The door of the elevator must be closed when it is in operation."); } @Override public void run() { System.out.println("The elevator is starting to run..."); } @Override public void stop() { System.out.println("The elevator is ready to go from Running State to Stoping state"); super.context.setLiftState(Context.stoppingState); super.context.getLiftState().stop(); } }
1.5. Elevator StoppingState subclass
package com.hlj.moudle.design.D08 Management status.D19Status State mode; /** * @author HealerJean * @ClassName StoppingState * @date 2019-08-20 21:31. * @Description What can I do in a stop state? */ public class StoppingState extends LiftState { @Override public void open() { System.out.println("The elevator is ready to go from Stopping State to Opening state"); super.context.setLiftState(Context.openningState); super.context.getLiftState().open(); } @Override public void close() { throw new RuntimeException("When the elevator stops, they're closed."); } @Override public void run() { System.out.println("The elevator is ready to go from Stopping State to Running state"); super.context.setLiftState(Context.runningState); super.context.getLiftState().run(); } @Override public void stop() { System.out.println("The elevator stopped...."); } }
1.6. State Application Object/Environment
public class Context { /** * Define all elevator States */ public final static OpenningState openningState = new OpenningState(); public final static ClosingState closeingState = new ClosingState(); public final static RunningState runningState = new RunningState(); public final static StoppingState stoppingState = new StoppingState(); /** * Set a current elevator status */ private LiftState liftState; public LiftState getLiftState() { return liftState; } /** * Binding state and environment */ public void setLiftState(LiftState liftState) { this.liftState = liftState; this.liftState.setContext(this); } public void open() { this.liftState.open(); } public void close() { this.liftState.close(); } public void run() { this.liftState.run(); } public void stop() { this.liftState.stop(); } }
1.7. Testing
public class Z19Main { public static void main(String[] args) { Context context = new Context(); context.setLiftState(new ClosingState()); context.open(); System.out.println("--------"); context.close(); System.out.println("--------"); context.run(); System.out.println("--------"); context.stop(); } }
3. Summary
The state mode, for me personally, is not much used, because the state I use in the scene is directly changed from one state to another. In fact, if you think about it carefully, you can also use the state mode. For example, there is a product in millet (close, open, contract to be configured, abandoned). When we close, if we open, we need to judge the contract needs to be configured, then we can open it. If the contract is configured, let him go to the contract to be matched. Position state,
In short, there will be more about the state in the future (the object's behavior depends on its state), which needs to be modified to determine the current state and, if it can be transited to another state, can be used.
Interested, welcome to add blogger Wechat
Ha, the blogger is very happy to communicate with friends from all walks of life. If you are satisfied, please reward the amount of money the blogger intends to pay. Interested in Wechat transfer, please note your Wechat or other contact information. Add the blogger Weixin.
Please leave a message below. Discuss freely with bloggers
Wechat Public Number | Alipay | |
---|---|---|