Java description design pattern (21): state pattern

Source code: GitHub point here || GitEE point here

I. life scene

1. Scene description

Chameleon is a reptile, is a very strange animal, it has a variety of characteristics and behavior suitable for arboreal life, the body will change with the change of the environment to adapt to the color of the environment, it is very magical. The following describes the change process based on the state mode.

2. Code implementation

public class C01_InScene {
    public static void main(String[] args) {
        Chameleon chameleon = new Chameleon("Red","Flower cluster environment") ;
        LifeContext lifeContext = new LifeContext() ;
        // Leaf environment
        BodyColor bodyColor = new GreenColor ();
        lifeContext.setBodyColor(bodyColor);
        lifeContext.change(chameleon);
        // Branch environment
        bodyColor = new GrayColor() ;
        lifeContext.setBodyColor(bodyColor);
        lifeContext.change(chameleon);
    }
}
/**
 * Chameleon
 */
class Chameleon {
    public String color ;
    public String contextDesc ;
    public Chameleon(String color, String contextDesc) {
        this.color = color;
        this.contextDesc = contextDesc;
    }
}
/**
 * Chameleon living environment
 */
class LifeContext {
    private BodyColor bodyColor;
    public void setBodyColor(BodyColor bodyColor) {
        this.bodyColor = bodyColor;
    }
    public void change (Chameleon chameleon){
        bodyColor.change(chameleon) ;
    }
}
/**
 * Chameleon body color abstract class
 */
interface BodyColor {
    void change (Chameleon chameleon);
}
/**
 * Body color of chameleon
 */
class GreenColor implements BodyColor {
    @Override
    public void change(Chameleon chameleon) {
        System.out.println("Before change:"+chameleon.color+";"+chameleon.contextDesc);
        chameleon.contextDesc = "Leaf environment" ;
        chameleon.color = "green" ;
        System.out.println("After the change:"+chameleon.color+";"+chameleon.contextDesc);
    }
}
class GrayColor implements BodyColor {
    @Override
    public void change(Chameleon chameleon) {
        System.out.println("Before change:"+chameleon.color+";"+chameleon.contextDesc);
        chameleon.contextDesc = "Branch environment" ;
        chameleon.color = "gray" ;
        System.out.println("After the change:"+chameleon.color+";"+chameleon.contextDesc);
    }
}

II. State mode

1. Basic concept

State mode is the behavior mode of an object. State mode allows an object to change its behavior when its internal state changes. State pattern encapsulates the behavior of objects in different state objects, and each state object is a subclass of abstract state class. The intention is to change the behavior of an object when its internal state changes.

2. Mode diagram

3. Core role

  • Environmental role

Holds an object instance of a concrete state class. The instance of this concrete state class gives the existing state of this environment object.

  • Abstract state role

Define an interface that encapsulates the behavior corresponding to the state of the environment object.

  • Specific status role

The concrete state class implements the behavior corresponding to the state of the environment.

4. Source code implementation

public class C02_State {
    public static void main(String[] args){
        Context context = new Context();
        State state = new ConcreteStateA() ;
        context.setState(state);
        context.printInfo("Current environment status A");
        state = new ConcreteStateB();
        context.setState(state);
        context.printInfo("Current environment status B");
    }
}
/**
 * Environmental role
 */
class Context {
    private State state;
    public void setState(State state) {
        this.state = state;
    }
    public void printInfo (String info) {
        state.stateInfo(info);
    }
}
/**
 * Abstract state role
 */
interface State {
    void stateInfo (String param);
}
/**
 * Specific status role
 */
class ConcreteStateA implements State {
    @Override
    public void stateInfo (String info) {
        System.out.println("ConcreteStateA: " + info);
    }
}
class ConcreteStateB implements State {
    @Override
    public void stateInfo (String info) {
        System.out.println("ConcreteStateB: " + info);
    }
}

III. model summary

  1. Split the if else statements that are easy to cause problems, and the state pattern encapsulates the behavior of each state into a corresponding class. The code has a strong readability.
  2. In line with the "opening and closing principle", it is easy to add or delete operations and manage the state.
  3. There will be many states. Each state needs a corresponding class, which will generate many classes and add dimensions
    Guard difficulty.
  4. Application scenario: when an event or object has many states, the States will switch to each other, and different states have different behaviors. You can consider using state mode.

IV. source code address

GitHub·address
https://github.com/cicadasmile/model-arithmetic-parent
GitEE·address
https://gitee.com/cicadasmile/model-arithmetic-parent

Keywords: Java github

Added by volomike on Fri, 22 Nov 2019 21:34:02 +0200