1. Introduction
1. Definition
State Pattern is a behavioral design pattern that allows an object to change its behavior when its internal state changes. By defining a state interface and a specific state class, the state and behavior of an object are separated, so that behavior can be changed when the state changes.
2. Structure of state mode
Status mode involves the following roles:
State (state interface): defines the common interface of the state class, and all specific state classes implement this interface.
ConcreteState (specific state class): Each specific state class implements the State interface and defines the specific behavior in this state.
Context: The context class maintains a reference to the current state, which is usually delegated to the specific state class to perform the corresponding state switching.
2. Java implementation case
1. Light switch status case
/** * @Description: Status interface * @Date: 2025-02-07 17:09 * @Author: gaoyufei **/ public interface State { void handle(Switch context); } /** * @Description: Turn off the light status * @Date: 2025-02-07 17:12 * @Author: gaoyufei **/ public class CloseState implements State{ @Override public void handle(Switch switcher) { (new OpenState()); ("The lights are on"); } } /** * @Description: Lighting status * @Date: 2025-02-07 17:11 * @Author: gaoyufei **/ public class OpenState implements State{ @Override public void handle(Switch switcher) { (new CloseState()); ("The lights are turned off"); } } /** * @Description: switch, holding the light status * @Date: 2025-02-07 17:08 * @Author: gaoyufei **/ public class Switch { private State state; public Switch(State state){ =state; } public void setState(State state){ =state; } public void press(){ (this); } } /** * @Description: Operator * @Date: 2025-02-07 17:14 * @Author: gaoyufei **/ public class Operator { public static void main(String[] args) throws InterruptedException { Switch switcher=new Switch(new CloseState());//The initial state is the light is off //Simulate the switch every 1 second while (true){ (1000); (); } } }
2. Pomodoro working method status case
/** * @Description: Tomato Status Interface * @Date: 2025-02-08 10:49 * @Author: gaoyufei **/ public interface TomatoState { void deal(Worker worker); } /** * @Description: Tomato - Working Status Class * @Date: 2025-02-08 10:51 * @Author: gaoyufei **/ public class WorkTomatoState implements TomatoState { @Override public void deal(Worker worker) { ("The rest is over, start working"); try { (25*60*1000); } catch (InterruptedException e) { (); } ("Working for 25 minutes"); (new RestTomatoState()); } } /** * @Description: Tomato - rest status class * @Date: 2025-02-08 10:53 * @Author: gaoyufei **/ public class RestTomatoState implements TomatoState{ @Override public void deal(Worker worker) { ("The work is over, have a good rest"); try { (5*60*1000); } catch (InterruptedException e) { (); } ("After 5 minutes' rest"); (new WorkTomatoState()); } } /** * @Description: Workers * @Date: 2025-02-08 10:51 * @Author: gaoyufei **/ public class Worker { private TomatoState tomatoState; public Worker(TomatoState tomatoState){ =tomatoState; } public void setTomatoState(TomatoState tomatoState){ =tomatoState; } public void startTomatoClock(){ (this); } } /** * @Description: Client * @Date: 2025-02-08 11:02 * @Author: gaoyufei **/ public class Client { public static void main(String[] args) { Worker worker=new Worker(new WorkTomatoState()); while (true){ (); } } }
3. Elevator status case
This is a simple elevator system. The elevator states that close the door, open, rise, fall, and stop. The behavior of the elevator in each state is different. Through the status mode, different behaviors are performed according to the different states of the elevator.
/** * @Description: Elevator status interface * @Date: 2025-02-08 9:02 * @Author: gaoyufei **/ public interface ElevatorState { void openDoor(); void closeDoor(); void moveUp(); void moveDown(); void stop(); } /** * @Description: Close status * @Date: 2025-02-08 9:05 * @Author: gaoyufei **/ public class CloseState implements ElevatorState { private Elevator elevator; public CloseState(Elevator elevator){ =elevator; } @Override public void openDoor() { ("The elevator door is open"); (()); } @Override public void closeDoor() { ("The elevator door is closed"); } @Override public void moveUp() { ("Elevator rises"); (()); } @Override public void moveDown() { ("Elevator descends"); (()); } @Override public void stop() { ("Elevator stops"); (()); } } /** * @Description: Door opening status * @Date: 2025-02-08 9:07 * @Author: gaoyufei **/ public class OpenState implements ElevatorState{ private Elevator elevator; public OpenState(Elevator elevator){ =elevator; } @Override public void openDoor() { ("The elevator door has been opened"); } @Override public void closeDoor() { ("The elevator door is closed"); (()); } @Override public void moveUp() { ("The elevator door is open and cannot rise"); } @Override public void moveDown() { ("The elevator door is open and cannot descend"); } @Override public void stop() { ("The elevator door is open, cannot be changed to a stop state"); } } /** * @Description: Stop status * @Date: 2025-02-08 9:59 * @Author: gaoyufei **/ public class StopState implements ElevatorState{ private Elevator elevator; public StopState(Elevator elevator){ =elevator; } @Override public void openDoor() { ("The elevator door is open"); (()); } @Override public void closeDoor() { ("The elevator door is closed"); (()); } @Override public void moveUp() { ("Elevator rises"); (()); } @Override public void moveDown() { ("Elevator descends"); (()); } @Override public void stop() { ("Elevator stops"); } } /** * @Description: Rising state * @Date: 2025-02-08 9:07 * @Author: gaoyufei **/ public class MovingUpState implements ElevatorState{ private Elevator elevator; public MovingUpState(Elevator elevator){ =elevator; } @Override public void openDoor() { ("The elevator is rising and the door cannot be opened"); } @Override public void closeDoor() { ("The elevator has been closed"); } @Override public void moveUp() { ("The elevator is rising"); } @Override public void moveDown() { ("Elevator descends"); (()); } @Override public void stop() { ("Elevator stops"); (()); } } /** * @Description: Descending state * @Date: 2025-02-08 9:08 * @Author: gaoyufei **/ public class MovingDownState implements ElevatorState{ private Elevator elevator; public MovingDownState(Elevator elevator){ =elevator; } @Override public void openDoor() { ("The elevator is descending and the door cannot be opened"); } @Override public void closeDoor() { ("The elevator is descending and has been closed"); } @Override public void moveUp() { ("Elevator rises"); (()); } @Override public void moveDown() { ("The elevator is descending"); } @Override public void stop() { ("Elevator stops"); (()); } } /** * @Description: Elevator * @Date: 2025-02-08 9:04 * @Author: gaoyufei **/ public class Elevator { private ElevatorState closeState; private ElevatorState openState; private ElevatorState movingDownState; private ElevatorState movingUpState; private ElevatorState stopState; private ElevatorState currentState; public Elevator(){ closeState=new CloseState(this); openState=new OpenState(this); movingDownState=new MovingDownState(this); movingUpState=new MovingUpState(this); stopState=new StopState(this); currentState=closeState;// The default initial state is off } public void setCurrentState(ElevatorState currentState) { = currentState; } public ElevatorState getCloseState() { return closeState; } public ElevatorState getOpenState() { return openState; } public ElevatorState getMovingDownState() { return movingDownState; } public ElevatorState getMovingUpState() { return movingUpState; } public ElevatorState getStopState() { return stopState; } public void openDoor(){ (); } public void closeDoor(){ (); } public void moveUp(){ (); } public void moveDown(){ (); } public void stop(){ (); } } /** * @Description: Client * @Date: 2025-02-08 9:29 * @Author: gaoyufei **/ public class Client { public static void main(String[] args) { //Initial Close Status Elevator elevator=new Elevator(); //close the door ();//Output The elevator door has been closed //Open the door ();//Output The elevator door is open //rise ();//Output The elevator door cannot be upgraded when it is open //close the door ();//Output The elevator door is closed //rise ();//Output Elevator rise //close the door ();//Output The elevator has been closed //decline ();//Output Elevator drop //stop ();//Output Elevator stops //Open the door ();//Output The elevator door is open //stop ();//Output The elevator door is open and cannot be changed to a stop state } }
This is the article about Java's sample code to implement state mode. For more related Java state mode content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!