preface
State mode is also behavioral design mode. The original intention of the design is to deal with the change of different behaviors when different states change in the same object.
directory
A, definitions,
When an object’s internal state changes, allowing it to change its behavior, the object looks as if it has changed its class. In simple terms, by defining a state variable, by which you control your behavior.
Two, mode principle analysis
Let’s simulate an elevator opening and closing the door
Public abstract class LiftState{// define a role that encapsulates the status change of the role. public void setContext(Context _context){ this.context = _context; } public abstract void open(); Public abstract void close(); Public abstract void run(); Public abstract void stop(); } public class Context{public final static ClosingState closeingState = new ClosingState(); public final static RunningState runningState = new RunningState(); public final static StoppingState stoppingState = new StoppingState(); Private LiftSate liftState; private LiftSate liftState; public LiftState getLiftState(){ return liftState; } public void setLiftState(LiftState liftState){ this.liftState = listState; 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(); Public class OpenngingState extends LiftState{@override public void close(){// Public void close() extends LiftState super.context.setLifeState(Context.closeingState); / / entrusted to CloseState to perform super. Context. GetLiftState (). The close (); } @override public void open(){system.out.println (" the door is open "); } @override public void run(){@override public void stop(){// Stop the elevator}} //4. Public class OpenngingState extends LiftState{@override public void close(){system.out.println (" the door is closed "); } @ Override public void open () {/ / state modify super. Context. SetLifeState (context. OpenningState); super.context.getLiftState().open(); } @Override public void run(){ super.context.setLifeState(Context.runingState); super.context.getLiftState().run(); } @Override public void stop(){ super.context.setLifeState(Context.stoppingState); super.context.getLiftState().stop(); Public class RunningState extends LifeState{@override public void close(){} @override public void open(){ } @override public void run(){system.out.println (" lift up and down "); } @Override public void stop(){ super.context.setLifeState(Context.stoppingState); super.context.getLiftState().stop(); Public class RunningState extends LifeState{@override public void close(){} @override public void open(){ super.context.setLifeState(Context.openningState); super.context.getLiftState().open(); } @Override public void run(){ super.context.setLifeState(Context.runningState); super.context.getLiftState().run(); } @override public void stop(){system.out.println (); Public class Client{public static void main(String[] args){Context Context = new Context(); context.setLiftState(new CloseingState()); context.open(); context.close(); context.run(); context.stop(); }}Copy the code
Iii. Usage Scenarios
-
Scenarios where behavior changes with state changes, such as changing purchase orders, elevator operating states
-
Alternative to conditional, branch judgment statements
Four advantages,
-
Avoid a large number of if-else statements and keep the structure clear
-
Quickly understand the relationship between states and behavior
-
Follow the design principle, each state is a subclass, new and modified for the subclass, reflecting the single responsibility principle
Five, the disadvantages
-
Many states will have many subclasses, resulting in many scattered classes,
-
The state switching relationship is complex, and the relationship between each state needs to be well understood, which increases the maintenance cost