1. State mode
Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. (When an object's internal state changes, allow it to change some behavior of the object so that the object looks like it has changed the class)
1.1 State Patterns - Core Ideas
1.2 State Patterns-Class Diagrams
The state model has three main roles:
- State: An abstract state role that defines state-related behavior. Context can be used as a parameter of a method.
- ConcreteState: Specific state, which implements different ways of behaving in that state
- Context: Environment role, maintenance of a State instance, support for state modification
1.4 State Patterns - Advantages and Disadvantages
- Advantage:
- Clear structure, avoiding excessive if-else, switch-case structure
- Following the principle of single responsibility and the principle of opening and closing
- Good encapsulation
- Disadvantages:
- Increasing state classes increases the number and type of classes in the system
- The structure and implementation of state mode are complex, and improper use will lead to confusion of program structure and code.
1.5 state mode - applicable scenario
- Scenarios in which behavior changes with state
- Conditions, Substitutes for Branch Judgment Statements
2. State Patterns - Examples of Applications
Class 2.1 Graphs
2.2 Elevator State Interface-ILiftState
Abstract elevator door opening and closing method
public interface ILiftState { public void open(); public void close(); }
2.2 Running State-LiftRunState
public class LiftRunState implements ILiftState { @Override public void open() { System.out.println("Elevator runs regularly and cannot open the door"); } @Override public void close() { System.out.println("Elevator runs regularly and cannot close elevator door"); } }
2.2 Stop State - LiftStop State
public class LiftStopState implements ILiftState { @Override public void open() { System.out.println("Open the elevator door..."); } @Override public void close() { System.out.println("Close the elevator door..."); } }
2.2 Elevator Class - ILiftState
public class Lift { private ILiftState liftState; public ILiftState getLiftState() { return liftState; } public void setLiftState(ILiftState liftState) { this.liftState = liftState; } public void openDoor() { this.liftState.open(); } public void closeDoor(){ this.liftState.close(); } }
2.2 Test
Setting up different states will result in different outputs
@Test public void test_state(){ Lift lift = new Lift(); lift.setLiftState(new LiftRunState()); lift.openDoor(); lift.closeDoor(); }
3. Contrast
If you don't use state mode, the code is probably written like this
public class Liftt { // running state public static final int STATE_RUN = 1; // Suspension status public static final int STATE_STOP = 0; private int liftState; public int getLiftState() { return liftState; } public void setLiftState(int liftState) { this.liftState = liftState; } public void close(){ switch (liftState) { case STATE_RUN: System.out.println("The elevator is running and the door is closed."); break; case STATE_STOP: System.out.println("Close the elevator door"); break; } } public void open() { switch (liftState) { case STATE_RUN: System.out.println("The elevator is running and cannot be opened."); break; case STATE_STOP: System.out.println("Open the elevator door"); break; } } }