Java state mode

Mode state (State Pattern), the behavior of the class is based on its state change. This type of design pattern belongs behavioral patterns.

In the state model, we create the context object represents the behavior of various objects and a state of change as the state of the object is changed.

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, we 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.

State.java

public interface State {// create an interface
    public void the doAction (the Context context);
}

Context.java

public class Context { //创建Context类
    private State state;
    
    public Context() {
        state = null;
    }
    
    public void setState(State state) {
        this.state = state;
    }
    
    public State getState() {
        return state;
    }
    
}

StartState.java

public class StartState implements State {// create the entity class implement an interface
    
    public void the doAction (Context context) {
        System.out.println ( "Player IS in Start State");
        context.setState (the this);
    }
    
    public String toString () {
        return "the Start State";
    }
    
}

StopState.java

public class StopState implements State {
    
    public void doAction(Context context) {
        System.out.println("Player is in stop state");
        context.setState(this);
    }
    
    public String toString() {
        return "Stop State";
    }
    
}

StatePatternDemo.java

public class StatePatternDemo {// Check to a Context State change behavior state changes
    
    public static void main (String [] args) {
        Context context = new new Context ();
        
        startState the startState property of startState new new = ();
        startState.doAction (context) ;
        
        System.out.println (context.getState () toString ().);
        
        StopState stopState new new StopState = ();
        stopState.doAction (context);
        
        System.out.println (. context.getState () toString ());
    }
    
}

 

 

Guess you like

Origin blog.csdn.net/walkerwqp/article/details/91802457