This article source: making here | | GitEE, click here

One, life scenes

1. Scene Description

Chameleon is a reptile, is a very strange animal, it has a variety of characteristics and behaviors suitable for the arboreal life, the body will change to adapt to the environment with the change of color, very amazing. The change process is described below based on state patterns.

2. Code implementation

public class C01_InScene {
    public static void main(String[] args) {
        Chameleon chameleon = new Chameleon("Red"."Flower environment"); LifeContext lifeContext =new LifeContext() ;
        // The leaf environment
        BodyColor bodyColor = new GreenColor ();
        lifeContext.setBodyColor(bodyColor);
        lifeContext.change(chameleon);
        // Tree environment
        bodyColor = newGrayColor() ; 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; }}/** * The chameleon's 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);
}
/** ** Chameleon body color specific class */
class GreenColor implements BodyColor {
    @Override
    public void change(Chameleon chameleon) {
        System.out.println("Before the change:"+chameleon.color+";"+chameleon.contextDesc);
        chameleon.contextDesc = "Leaf environment" ;
        chameleon.color = "Green" ;
        System.out.println("After change:"+chameleon.color+";"+chameleon.contextDesc); }}class GrayColor implements BodyColor {
    @Override
    public void change(Chameleon chameleon) {
        System.out.println("Before the change:"+chameleon.color+";"+chameleon.contextDesc);
        chameleon.contextDesc = "Tree environment" ;
        chameleon.color = "Gray" ;
        System.out.println("After change:"+chameleon.color+";"+chameleon.contextDesc); }}Copy the code

Second, state mode

1. Basic concepts

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

2. Pattern diagram

3. Core roles

  • Environmental role

An object instance that holds a concrete state class. An instance of this concrete state class gives the existing state of the environment object.

  • Abstract state role

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

  • Specific state 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 State A");
        state = new ConcreteStateB();
        context.setState(state);
        context.printInfo("Current environment state B"); }}/** * Environment 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); }}Copy the code

3. Model summary

  1. Split problematic if-else statements, and state mode encapsulates the behavior of each state into a corresponding class, making the code very readable.
  2. In line with the “open and close principle”, easy to add and delete operations, management status.
  3. There are a lot of states. Each state needs a corresponding class, which will produce many classes and increase the difficulty of maintenance.
  4. Application scenario: When an event or object has multiple states, the states will be converted to each other. Different states have different behaviors. Therefore, you can use the state mode.

Source code address

Making address https://github.com/cicadasmile/model-arithmetic-parent GitEE · https://gitee.com/cicadasmile/model-arithmetic-parentCopy the code