Pattern state

I. Introduction of models

1.1 definition

Allows an object to change its behavior when its internal state changes, and the object appears to have modified its class.

State mode packages the behavior of the object changed by the environment in different state objects. Its intention is to make an object change its behavior when its internal state changes.

1.2 advantages

  1. The structure is clear, and the state mode localizes the behavior related to a specific state into one state, and separates the behavior of different states to meet the principle of single responsibility
  2. Display the state transition to reduce the interdependence between objects. Introducing different states into independent objects will make the state transition more explicit and reduce the interdependence between objects
  3. Clear responsibilities in the same category are conducive to the expansion of procedures. It is easy to add new states and transitions by defining new subclasses

1.3 disadvantages

  1. The use of state mode will inevitably increase the number of classes and objects in the system
  2. The structure and implementation of state mode are complex. Improper use will lead to confusion of program structure and code
  3. The state mode does not support the opening and closing principle very well. For the state mode that can switch states, adding a new state class requires modifying the source code responsible for state transformation, otherwise it cannot switch to the new state, and modifying the behavior of a state class also requires modifying the source code of the corresponding class

2, Structure and Implementation

2.1 structure

  1. Context: also known as context, it defines the interface required by the client, internally maintains a current state, and is responsible for state switching
  2. Abstract State role: define an interface to encapsulate the behavior corresponding to a specific State in an environment object. There can be one or more behaviors
  3. Concrete state: implement the behavior corresponding to the abstract state and switch the state if necessary

2.2 realization

2.2.1,State

package com.erlang.state;

/**
 * @description: Abstract state role
 * @author: erlang
 * @since: 2022-02-17 22:19
 */
public abstract class State {

    /**
     * State processing method
     *
     * @param context context
     */
    public abstract void handle(Context context);
}

2.2.2,ConcreteState1

package com.erlang.state;

/**
 * @description: Specific status class 1
 * @author: erlang
 * @since: 2022-02-17 22:20
 */
public class ConcreteState1 extends State {

    @Override
    public void handle(Context context) {
        System.out.println("Status 1 execution!");
        context.setState(new ConcreteState2());
    }
}

2.2.3,ConcreteState2

package com.erlang.state;

/**
 * @description: Specific status class 2
 * @author: erlang
 * @since: 2022-02-17 22:20
 */
public class ConcreteState2 extends State {

    @Override
    public void handle(Context context) {
        System.out.println("Status 2 execution!");
        context.setState(new ConcreteState1());
    }
}

2.2.4,Context

package com.erlang.state;

/**
 * @description: Environment class
 * @author: erlang
 * @since: 2022-02-17 22:20
 */
public class Context {

    /**
     * state
     */
    private State state;

    public Context() {
        this.state = new ConcreteState1();
    }

    /**
     * Set new status
     *
     * @param state state
     */
    public void setState(State state) {
        this.state = state;
    }

    /**
     * Read status
     *
     * @return Return status
     */
    public State getState() {
        return (state);
    }

    /**
     * Processing requests
     */
    public void handle() {
        state.handle(this);
    }

}

2.2.5,StateClient

package com.erlang.state;

/**
 * @description: State mode client test
 * @author: erlang
 * @since: 2022-02-17 22:23
 */
public class StateClient {
    public static void main(String[] args) {
        // Create environment
        Context context = new Context();
        //Processing requests
        context.handle();
        context.handle();
        context.handle();
        context.handle();
    }
}

2.2.6 implementation results

The current status is 1!
The current status is 2!
The current status is 1!
The current status is 2!

Keywords: Design Pattern

Added by grglaz on Thu, 17 Feb 2022 23:32:39 +0200