State Design Pattern in Java


Status Mode

In the State Pattern, the behavior of a class is changed based on its state. This type of design pattern is a behavioral model.

In state mode, we create objects that represent various states and a context object whose behavior changes as the state object changes.

Introduction

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

Main solution: The behavior of an object depends on its state (attribute) and its behavior can be changed based on its state change.

When to use: The code contains a number of conditional statements related to the state of the object.

How to solve: Abstract various concrete state classes.

Key Code: There is usually only one method in the interface of the command mode. There are one or more methods in the state mode interface. Moreover, the state mode implementation class method generally returns a value, or changes the value of an instance variable. That is, the state mode is generally related to the state of the object. The methods that implement classes have different functions, covering methods in the interface. State mode, like command mode, can also be used to eliminate conditional select statements such as if...else.

Application example: 1. When playing basketball, athletes can have normal, abnormal and abnormal conditions. 2, Zeng Houyi bells in the clock, 'clock is the abstract interface', 'clock A' and so on is the specific state, 'Zeng Houyi chime' is the specific environment (Context).

Advantages: 1. Encapsulate the conversion rules. 2. Enumerate the possible states and determine the state type before enumerating the state. 3. Put all the behaviors related to a certain state into a class, and you can easily add new states. You only need to change the state of the objects to change the behavior of the objects. 4. Allow state transition logic to be integrated with state objects, rather than a huge conditional statement block. 5, can allow multiple environment objects to share a state object, thereby reducing the number of objects in the system.

Disadvantages: 1, the use of state mode will inevitably increase the number of system classes and objects. 2, the structure and implementation of the state mode are more complicated, if used improperly will lead to confusion of the program structure and code. 3, the state mode support for the "opening and closing principle" is not very good, for the state mode that can switch state, adding a new state class needs to modify the source code responsible for the state transition, otherwise it can not switch to the new state, and modify The behavior of a state class also needs to modify the source code of the corresponding class.

Usage scenarios: 1. A scenario in which behavior changes as the state changes. 2. Replacement of conditions and branch statements.

Note: Use state mode when the behavior is constrained by state, and the state does not exceed five.

Implementation

We will create an State interface and an entity state class that implements the State interface. Context is a class with a state.

StatePatternDemo, our demo class uses Context and state objects to demonstrate how the Context changes behavior when the state changes.

state_pattern_uml_diagram

Step 1

Create an interface.

State.java

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

Step 2

Create an 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

Create the Context class.

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

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

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

Executing the program, outputting results:

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





javacodegeeks is optimized for learning.© javacodegeeks .
All Right Reserved and you agree to have read and accepted our term and condition