JavaScript es6设计模式 – Command(命令模式)

46次阅读
没有评论

Command(命令模式)

将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可取消的操作。

'use strict';

class Invoker {
    constructor() {
        console.log('Invoker Class created');
    }

    storeCommand(command) {
        this.command = command;
        console.log('Invoker.storeCommand invoked');
    }
}

class Command {
    constructor() {
        console.log('Command Class created');
    }

    execute() {
        console.log('Command.execute invoked');
    }
}

class ConcreteCommand extends Command {
    constructor(receiver, state) {
        super();
        this.receiver = receiver;
        console.log('ConcreteCommand Class created');
    }

    execute() {
        console.log('ConcreteCommand.execute invoked');
        this.receiver.action();
    }
}

class Receiver {
    constructor() {
        console.log('Receiver Class created');
    }

    action() {
        console.log('Receiver.action invoked');
    }
}

var invoker = new Invoker();
var receiver = new Receiver();
var command = new ConcreteCommand(receiver);
invoker.storeCommand(command);
invoker.command.execute();