"Head-First- design mode" notes 14- state mode

Mode state (State Pattern): allows an object to change its behavior when its internal state is changed, the object will appear to change its class. Alias ​​state objects (Objects for States), the state of the object model is a behavioral pattern.

In many cases, the behavior of an object depends on one or more attributes dynamic, such a property is called the state, such an object is called stateful (stateful) objects, such as object state is engaged in a series of pre-defined values ​​removed. When one of these objects interact with external events, it will change its internal state, so that the behavior of the system also will be changed.

In some cases more environmental objects need to share the same state, if you want to achieve more environmental objects in the system instances to share one or more state object, you need to define the state of the object as a static member object environment.

Class Diagram

  • Context: Environmental
  • State: state abstract class
  • ConcreteState: Specific Status Class

State mode

State abstract class status

This uses the interface, if there is a common feature to use an abstract class.

public interface State {
    /**
     * 状态对应的处理
     */
    public void handle(String sampleParameter);
}

Environmental Context

public class Context {
    private State stateA; // 所有状态
    private State stateB;

    // 持有一个 State 类型的对象实例,当前状态
    private State state;

    public Context() {
        stateA = new ConcreteStateA(this);
        stateB = new ConcreteStateB(this);
        state = stateA;
    }

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

    /**
     * 用户感兴趣的接口方法
     */
    public void request(String sampleParameter) {
        // 转调state来处理
        state.handle(sampleParameter);
    }

    public State getStateA() {
        return stateA;
    }

    public State getStateB() {
        return stateB;
    }
}

The first state

public class ConcreteStateA implements State {
    Context context;

    public ConcreteStateA(Context context) {
        this.context = context;
    }

    @Override
    public void handle(String sampleParameter) {
        System.out.println("具体状态A 处理 :" + sampleParameter);
        context.setState(context.getStateB()); // 完成操作后,切换状态
    }
}

The second state

public class ConcreteStateB implements State {
    Context context;

    public ConcreteStateB(Context context) {
        this.context = context;
    }

    @Override
    public void handle(String sampleParameter) {
        System.out.println("具体状态B 处理 :" + sampleParameter);
        context.setState(context.getStateA()); // 完成操作后,切换状态
    }
}

test

public class Client {
    public static void main(String[] args) 大专栏  《Head-First-设计模式》笔记14-状态模式{
        Context context = new Context();
        context.request("测试参数");
        context.request("测试参数");
        context.request("测试参数");
    }
}/*output:
具体状态A 处理 :测试参数
具体状态B 处理 :测试参数
具体状态A 处理 :测试参数
*/

The difference between state model and strategy mode

Status Mode : We will conduct a group package in state object, context behavior at any time may be entrusted to one of those states object. Over time, the current state of migration change in the status object in the collection to reflect the internal state of the context, therefore, context behavior will also change;

Strategy Mode : Customers often take the initiative to be specified Policy Object Context combination which one. Now, of course, the policy model allows us flexible, able to change the policy at runtime. But for a certain context objects, usually only one of the most appropriate policy object.

In general, we think of the strategy pattern is a flexible alternative in addition to inheriting away. If you use inheritance defines the behavior of a class, you will be trapped by this behavior, even his very difficult to modify. With the strategy mode, you can change the behavior by combining different objects;

We would like to state mode is not an alternative to a number of conditions be placed in the context of the judgment. By the behavior of the package into the state object, you can simply modify the state of the object by changing the context within the context of behavior;

advantage

  • Packaging conversion rules.
  • Enumeration of possible states, need to determine the status of species in the state before the enumeration.
  • 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 the object.
  • Allows the state transition logic state of the object in one, rather than one huge conditional statement block.
  • Environment allows multiple objects share a state object, thereby reducing the number of objects in the system.

Shortcoming

  • Bound state pattern is used to increase the number of system classes and objects.
  • Structure and implementation of the state model are more complex, if used improperly will lead to chaos and structure of the program code.
  • State mode support "on-off principle" is not very good for the state of the mode can be switched states, adding new classes need to modify the source code status of those responsible for state transitions, or can not switch to the new state; and a modified behavioral state classes must be amended source code corresponding to the class.

For the environment

  • The behavior of an object depends on its state (attributes) and may change based on its state and change its related behavior.
  • Code contains a number of conditional statements about the status of the object, the emergence of these conditional statements, lead to code maintainability and flexibility worse, can not easily add and delete state, the coupling between the client and class library enhancements . It contains object behavior statements in these conditions, and these conditions correspond to various states of the object.


Reproduced please indicate the source, welcome to the article cited sources of research, please point out any errors or expression is not clear enough. In the comments section below you can comment or e-mail to [email protected]

Guess you like

Origin www.cnblogs.com/wangziqiang123/p/11724541.html