Design Pattern Summary State Pattern (State)

The state mode (State) is that according to the state of the object, there will be different behaviors. Allows to change behavior when an object's internal state changes, the object appears to have changed its class.

The state mode solves the problem of complex and long logical judgment statements in the code, and the specific state role encapsulates the specific state and its corresponding behavior, which makes it very simple to add a new state. However, each state corresponds to a specific state class, the structure is scattered, the logic is not very clear, and the workload of reading the code will be larger.

 

    // The Context class maintains an instance of the ConcreteState subclass, which defines the current state.
    public class Context
    {
        private State state;
        // Define the initial state of the Context
        public Context(State state)
        {
            this.state = state;
        }

        // Process the request and set the next state
        public void Request()
        {
            state.Handle(this);
        }

        // Readable state property for reading and setting new state
        public void setState(State state){
            this.state= state;
        }

        public State getState(){
            return state;
        }
    }

    // Abstract state class that defines an interface to encapsulate behavior related to a specific state of the Context
    public abstract class State
    {
        public abstract void Handle(Context context);
    }

    // Concrete state class, each subclass implements a behavior related to a state of the Context
    public class ConcreteStateA implements State
    {
        
        // Set the next state of ConcreteStateA to be ConcreteStateB
        public void Handle(Context context)
        {
            System.out.println("The current state is A.");
            context.setState(new ConcreteStateB());
        }
    }

    public class ConcreteStateB implements State
    {
        // Set the next state of ConcreteStateB to be ConcreteSate
        public void Handle(Context context)
        {
            System.out.println("The current state is B.");
            context.setState(new ConcreteStateA());
        }
    }
 
class Program
    {
        static void Main(string[] args)
        {
            // Set the initial state of Context to ConcreteStateA
            Context context = new Context(new ConcreteStateA());

            // keep making requests while changing state
            context.Request();
            context.Request();
            context.Request();
            context.Request();
        }
    }
 

 

advantage

 The 1-state pattern localizes the behavior associated with a particular state and separates the behavior of different states.

 2 All state-related code exists in a ConcereteState, so it is easy to add new states and transitions by defining new subclasses.

 3 The state pattern reduces mutual dependencies by dividing various state transition logics into subclasses of State.

shortcoming

 Causes more ConcreteState subclasses

Applicable scene

 1 When the behavior of an object depends on its state, and it must change its behavior at runtime according to the state, the state pattern can be considered.

 2 An operation contains a huge branch structure, and these branches are determined by the state of the object.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326991934&siteId=291194637