JavaScript es6设计模式 – Chain of Responsibility(责任链模式)

44次阅读
没有评论

Chain of Responsibility(责任链模式)

为解除请求的发送者和接收者之间耦合,而使多个对象都有机会处理这个请求。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它。

'use strict';
class Handler {
    constructor() {
        console.log('Handler Class created');
    }

    handleRequest() {
        console.log('Handler.handleRequest invoked');
    }
}

class ConcreteHandler1 extends Handler {
    constructor() {
        super();
        console.log('ConcreteHandler1 Class created');
    }

    setSuccessor(successor) {
        this.successor = successor;
        console.log('ConcreteHandler1.setSuccessor invoked');
    }

    handleRequest(request) {
        console.log('ConcreteHandler1.handleRequest invoked');
        if (request === 'run')
            console.log('ConcreteHandler1 has handled the request');
        else {
            console.log('ConcreteHandler1 calls his successor');
            this.successor.handleRequest(request);
        }
    }
}

class ConcreteHandler2 extends Handler {
    constructor() {
        super();
        console.log('ConcreteHandler2 Class created');
    }

    handleRequest(request) {
        console.log('ConcreteHandler2.handleRequest invoked');
    }
}

let handle1 = new ConcreteHandler1();
let handle2 = new ConcreteHandler2();
handle1.setSuccessor(handle2);
handle1.handleRequest('run');
handle1.handleRequest('stay');