Behavioral patterns - state mode

State mode

Mode state (State Pattern), the behavior of the class is based on its state change.

Introduction

Intent: allows the object to change its behavior when the internal state is changed, the object appears to modify its class.

The main solution: an object's behavior depends on its state (property), and may be changed to change its behavior based on its associated state.

When to use: the code contains a number of conditional statements about the status of the object.

How to solve: the variety of specific state class abstracts.

The key code: only one way interfaces are typically command mode. While one or more of the interface state mode. Further, the class method implementation state mode, usually the return value, or change the value of the instance variable. In other words, the state and the state of the object model is generally related. The method of implementation class have different functions, a method of covering the interface. State mode and command mode the same can also be used to eliminate other conditions if ... else selection statement.

Application examples:  1, playing basketball athletes have normal, abnormal and extraordinary state status. 2, Bianzhong, the 'clock is the abstract interface', 'Clock A' and the like are specifically state, "Bianzhong 'specific environment (Context).

Advantages:  1, the conversion rule package. 2, enumerate the possible states, need to determine the status of species in the state before the enumeration. 3, all with a state-related behavior into a class, and you can easily add new state, only need to change the state of an object can change the behavior of objects. 4, allows the state transition logic state of the object in one, rather than one huge conditional statement block. 5, the environment allows multiple objects share a state object, thereby reducing the number of objects in the system.

Disadvantages:  1, using the state model systems inevitably increasing the number of classes and objects. 2, the structure and implementation of the state model are more complex, if used incorrectly will result in confusion structures and code of the program. 3, the state mode support for "opening and closing principle" is not very good for the state of the mode can be switched states, adding new classes need to modify the source code status of those responsible for state transitions, or can not switch to the new state, and modify a behavior state of the class corresponding to the class also need to modify the source code.

Usage scenarios:  1, depending on the state to change behavior and change of scene. 2, condition, instead of by the switch statement.

Note: Use state mode when the behavior of constrained by the state, and the state no more than five.

achieve

We will create a  State  interface and implementation of the  State  entity state class interface. Context  is a class with a state.

StatePatternDemo , our demonstration classes use  Context  and state objects to demonstrate changes in behavior in the Context state change.

 

 1, to create an interface

public interface State {

    public void doAction(Context context);
}

2. Create a class that implements the interface entity

public class StartState implements State {

    @Override
    public void doAction(Context context) {
        System.out.println("Player is in start stage");
        context.setState(this);
    }

    @Override
    public String toString() {
        return "Start State";
    }
}
public class StopState implements State {

    @Override
    public void doAction(Context context) {
        System.out.println("Player is in stop state");
        context.setState(this);
    }

    @Override
    public String toString() {
        return "Stop State";
    }
}

3. Create Context class

public class Context {
    private State state;

    public Context() {
    }

    public void setState(State state) {
        this.state = state;
    }

    public State getState() {
        return state;
    }
}

4. Use Context to see changes in behavior when the state changed the State

public class StatePatternDemo {
    public static void main(String[] args) {
        Context context = new Context();

        StartState startState = new StartState();
        startState.doAction(context);
        System.out.println(context.getState());

        StopState stopState = new StopState();
        stopState.doAction(context);
        System.out.println(context.getState());
    }
}

State When the state changes, toString methods of the object has changed.

Export

 

 

Reference: https://www.runoob.com/design-pattern/state-pattern.html

Guess you like

Origin www.cnblogs.com/hoo334/p/12522547.html