Java design patterns described in (21): Status Mode

This article Source: GitHub · Click here || GitEE · Click here

A living scene

1, scene description

Chameleon is a reptile, is very strange animal, which has a variety of features and behaviors suitable for arboreal life, the body will change as the environment changes the color to adapt to the environment, it's fantastic. Mode is described below on the basis of the state change process.

2, code implementation

public class C01_InScene {
    public static void main(String[] args) {
        Chameleon chameleon = new Chameleon("红色","花丛环境") ;
        LifeContext lifeContext = new LifeContext() ;
        // 树叶环境
        BodyColor bodyColor = new GreenColor ();
        lifeContext.setBodyColor(bodyColor);
        lifeContext.change(chameleon);
        // 树枝环境
        bodyColor = new GrayColor() ;
        lifeContext.setBodyColor(bodyColor);
        lifeContext.change(chameleon);
    }
}
/**
 * 变色龙
 */
class Chameleon {
    public String color ;
    public String contextDesc ;
    public Chameleon(String color, String contextDesc) {
        this.color = color;
        this.contextDesc = contextDesc;
    }
}
/**
 * 变色龙生存环境
 */
class LifeContext {
    private BodyColor bodyColor;
    public void setBodyColor(BodyColor bodyColor) {
        this.bodyColor = bodyColor;
    }
    public void change (Chameleon chameleon){
        bodyColor.change(chameleon) ;
    }
}
/**
 * 变色龙身体颜色抽象类
 */
interface BodyColor {
    void change (Chameleon chameleon);
}
/**
 * 变色龙身体颜色具体类
 */
class GreenColor implements BodyColor {
    @Override
    public void change(Chameleon chameleon) {
        System.out.println("变化前:"+chameleon.color+";"+chameleon.contextDesc);
        chameleon.contextDesc = "树叶环境" ;
        chameleon.color = "绿色" ;
        System.out.println("变化后:"+chameleon.color+";"+chameleon.contextDesc);
    }
}
class GrayColor implements BodyColor {
    @Override
    public void change(Chameleon chameleon) {
        System.out.println("变化前:"+chameleon.color+";"+chameleon.contextDesc);
        chameleon.contextDesc = "树枝环境" ;
        chameleon.color = "灰色" ;
        System.out.println("变化后:"+chameleon.color+";"+chameleon.contextDesc);
    }
}

Second, the state mode

1, the basic concept

State model behavior pattern of the object, an object model allows the state to change its behavior when its internal state changes. The behavior of an object state model objects encapsulated in different states, each state of the state objects are subclasses of the abstract class. The intent is to allow an object to change its internal state when their behavior has changed.

2, illustrates a pattern

3, the core role

  • Environmental role

Examples of specific state of holding the object class. Examples of this particular class is given the state of the current state of the object in this environment.

  • Abstract state role

Defining an interface, the package state of the environment corresponding to the behavior of the object.

  • The specific role of the state

Class implements a particular state corresponding to the state of the environment behavior.

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("当前环境状态A");
        state = new ConcreteStateB();
        context.setState(state);
        context.printInfo("当前环境状态B");
    }
}
/**
 * 环境角色
 */
class Context {
    private State state;
    public void setState(State state) {
        this.state = state;
    }
    public void printInfo (String info) {
        state.stateInfo(info);
    }
}
/**
 * 抽象状态角色
 */
interface State {
    void stateInfo (String param);
}
/**
 * 具体状态角色
 */
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);
    }
}

Third, the model summary

  1. if-else statement will be readily resolved the problem, the state of behavior of each mode is encapsulated into a state corresponding to a class, the code has a highly readable.
  2. In line with "the principle of opening and closing," it is easy to add or delete operation, management status.
  3. When there will be a lot of state. Each state must be a corresponding class, we will have a lot of class, increase maintenance
    care difficult.
  4. Scenario: When an event or object has a variety of state, will be interchangeable between states, different states have different behavior, consider using state mode.

Fourth, the source address

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

Guess you like

Origin www.cnblogs.com/cicada-smile/p/11914655.html