JavaScript es6设计模式 – State(状态模式)

53次阅读
没有评论

State(状态模式)

允许一个对象在其内部状态改变时改变它的行为。对象看起来似乎修改了它所属的类。

'use strict';

class Context {
    constructor(state) {
        console.log("Context Class created");
        switch (state) {
            case "A":
                this.state = new ConcreteStateA()
                break
            case "B":
                this.state = new ConcreteStateB()
                break
            default:
                this.state = new ConcreteStateA()
        }
    }

    request() {
        console.log('Context.request invoked');
        this.state.handle(this);
    }
}

class State {
    constructor() {
        console.log("State Class created");
    }

    handle() {
        console.log('State.handle invoked');
    }
}

class ConcreteStateA extends State {
    constructor() {
        super();
        console.log("ConcreteStateA Class created");
    }

    handle(context) {
        console.log('ConcreteStateA.handle invoked');
    }
}

class ConcreteStateB extends State {
    constructor() {
        super();
        console.log("ConcreteStateB Class created");
    }

    handle(context) {
        console.log('ConcreteStateB.handle invoked');
    }
}

let context = new Context("A")
context.request()