Latest web development tutorials

State pattern

In the state mode (State Pattern), the behavior of the class is based on its status changed. This type of design patterns belong behavioral patterns.

In the state model, we create objects and a variety of behavioral states along with state of the object changes altered context object representation.

Introduction

Intent: Allows an object to change its behavior in the internal state is changed, the object appears to modify its class.

Mainly to solve: an object's behavior depends on its state (attributes), and you can change it according to its state-related behavior change.

When to use: Code contains a large number of objects related to the status of conditional statements.

How to fix: the status of specific classes abstract out.

The key code: the command mode interface usually only one method.The status of the interface that has one or more methods. Moreover, the method of implementation class state mode, usually the return value, or change the value of an instance variable. That is, the state and the state of the object model is generally relevant. Implementation class methods have different functions, interface methods covered. State mode and command mode the same can also be used to eliminate other conditions if ... else selection statement.

Application examples: 1, playing basketball player can have a normal state, not the normal state and abnormal state.2, Marquis Yi of Zeng bells, the 'clock is abstract interface', 'clock A' and other specific state, 'Bianzhong' specific environment (Context).

Advantages: 1, encapsulates the transformation rules.2, enumerate possible states before enumeration state needs to determine the status of species. 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, allowing the state transition logic state of the object in one, rather than one huge block of conditional statements. 5, allows multiple objects share a state object environment, thereby reducing the number of objects in the system.

Disadvantages: 1, the state pattern of use is bound to increase the number of system classes and objects.2, the structure and implementation of the state pattern are more complex, if used incorrectly can cause confusion program structure and code. 3, support for the state pattern "Open Closed Principle" is not very good, you can switch the state of the state model, adding new classes need to modify the status of those responsible for state transitions of the source code, or can not switch to the new state, and modify a state class act also need to modify the source code of the corresponding class.

Usage scenarios: 1, with the state change behavior and change of scene.2, conditional branch statement substitute.

Note: When using a constrained state behavior by state mode, and the state is not more than five.

achieve

We will create aStateentity status interface and implementation classStateinterface.Contextis a class with a certain state.

StatePatternDemo,we demonstrate the use of class and statusContextContext objects to demonstrate behavior change in the state of change.

State pattern UML diagram

step 1

Create an interface.

State.java

public interface State {
   public void doAction(Context context);
}

Step 2

Create entity class that implements the interface.

StartState.java

public class StartState implements State {

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

   public String toString () {
      return "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";
   }
}

Step 3

CreateContextclass.

Context.java

public class Context {
   private State state;

   public Context () {
      state = null;
   }

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

   public State getState () {
      return state;
   }
}

Step 4

UseContextto see the behavior when the state changesStatechanges.

StatePatternDemo.java

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 () toString ().);

      StopState stopState = new StopState ();
      stopState.doAction (context);

      System.out.println (context.getState () toString ().);
   }
}

Step 5

Verify output.

Player is in start state
Start State
Player is in stop state
Stop State