This is the 24th day of my participation in the August Text Challenge.More challenges in August

The state pattern

For stateful objects, complex "judgment logic" is extracted into different state objects, allowing state objects to change their behavior when their internal state changes.Copy the code

structure

Context role: Also known as Context, it defines the interface required by the client, maintains a current state, and delegates state-related operations to the current state object.

Abstract State role: Defines an interface that encapsulates the behavior of a particular State in an environment object.

Concrete State role: Implements the behavior of the abstract State and switches the State if necessary.

demo

1. Environmental roles

public class Context {
    private State state;
    public Context(a) {
        this.state = new Orders();
    }
    public void setState(State state) {
        this.state = state;
    }
    public void handler(a) {
        this.state.handler(this); }}Copy the code

2. Abstract roles

public abstract class State {
    public abstract void handler(Context context);
}
Copy the code

3. Specific roles

public class Orders extends State {
    @Override
    public void handler(Context context) {
        System.out.println("Order successfully placed! Enter the preparatory phase -");
        context.setState(newSetout()); }}public class Setout extends State {
    @Override
    public void handler(Context context) {
        System.out.println("Preparation completed! Start to enter the processing stage - "");
        context.setState(newProcess()); }}public class Process extends State {
    @Override
    public void handler(Context context) {
        System.out.println("Processing stage complete! Begin to enter the completion phase -");
        context.setState(newFinish()); }}public class Finish extends State{
    @Override
    public void handler(Context context) {
        System.out.println("Completion stage complete! Start to enter the delivery phase -");
        context.setState(newConsignment()); }}public class Consignment extends State {
    @Override
    public void handler(Context context) {
        System.out.println("Successful delivery! End of process!"); }}Copy the code

4. Client

public class Client {
    public static void main(String[] args) {
        Context context = new Context();
        context.handler(); // Order placed successfully! Start to prepare -
        context.handler(); // The preparation stage is complete! Start to enter the processing stage -
        context.handler(); // The processing stage is complete! Begin to enter the completion phase -
        context.handler(); // The completion stage is complete! Start to enter the delivery phase -
        context.handler(); // Successful shipment! End of process!}}Copy the code

conclusion

advantages

1, separate the different states, and the behavior related to this state is controlled by the current situation, to meet the single responsibility principle

2. Reduce dependencies between classes and make state transitions more explicit

3. Easy to add and delete

disadvantages

1. Too many states will increase the number of system classes and increase system complexity

2, the structure and implementation are more complex, if used improperly will lead to the confusion of the program structure and code

3, the “open and close principle” support is not very good, to increase, delete, modify the need to modify the corresponding state source code