Home > Web Front-end > JS Tutorial > Detailed explanation of js chain of responsibility model

Detailed explanation of js chain of responsibility model

小云云
Release: 2018-02-28 14:22:42
Original
1675 people have browsed it

The chain of responsibility pattern refers to a request that requires multiple objects to be processed, thereby avoiding the coupling relationship between the sender and receiver of the request. These objects are connected into a chain and the request is passed along the chain until an object handles it.

It can be found from examples in life that a certain request may require approval from several people. Even if the technical manager has completed the approval, it still needs approval from the next level. For example, if the leave is less than 3 days in the company, the direct leader can approve it. If it is between 3 and 7 days, it needs the approval of the project manager. If it is more than 7 days, it needs the approval of the technical director. Having introduced so many examples of the chain of responsibility pattern in life, the definition of the chain of responsibility pattern in object-oriented is given below.

The objects involved in the responsibility chain model only have the processor role, but since there are multiple processors, they have a common method of processing requests, so an abstract processor role is abstracted here for code reuse.

Detailed explanation of js chain of responsibility model

Implementation

  • Request class

    // 采购请求
    let PurchaseRequest = function (amount, productName) {
        this.amount = amount;
        this.productName = productName;
    };
    Copy after login
  • Handler interface Class

    // 审批人,Handler
    let Approver = function (name, nextApprover) {
        this.name = name;
        this.nextApprover = nextApprover;
    };
    Approver.prototype.processRequest = function () {
        throw new Error();  
    };
    Copy after login
  • Processor Class

    // ConcreteHandler
    let Manager = function (name, nextApprover) {
        Approver.call(this, name, nextApprover);
    };
    extend(Manager, Approver);
    Manager.prototype.processRequest = function (request) {
        if (request.Amount < 10000.0)
        {
            console.log('ok');
        }
        else if (NextApprover != null)
        {
            this.nextApprover.ProcessRequest(request);
        }   
    };    
        
    // ConcreteHandler,副总
    let VicePresident = function (name, nextApprover) {
        Approver.call(this, name, nextApprover);
    };
    extend(VicePresident, Approver);
    VicePresident.prototype.processRequest = function (request) {
        if (request.Amount < 25000.0)
        {
            console.log('ok');
        }
        else if (NextApprover != null)
        {
            this.nextApprover.ProcessRequest(request);
        }   
    };
    
    
    // ConcreteHandler,总经理
    let President = function (name, nextApprover) {
        Approver.call(this, name, nextApprover);
    };
    extend(President, Approver);
    President.prototype.processRequest = function (request) {
        if (request.Amount < 100000.0)
        {
            console.log('ok');
        }
        else if (NextApprover != null)
        {
            this.nextApprover.ProcessRequest(request);
        }   
    };
    Copy after login

Test

let requestTelphone = new PurchaseRequest(4000.0, "Telphone");
let requestSoftware = new PurchaseRequest(10000.0, "Visual Studio");
let requestComputers = new PurchaseRequest(40000.0, "Computers");

let manager = new Manager("LearningHard");
let Vp = new VicePresident("Tony");
let Pre = new President("BossTom");

// 设置责任链
manager.NextApprover = Vp;
Vp.NextApprover = Pre;

// 处理请求
manager.ProcessRequest(requestTelphone);
manager.ProcessRequest(requestSoftware);
manager.ProcessRequest(requestComputers);
Copy after login

Usage Scenario

  • When the approval of a system requires multiple objects to complete processing, such as a leave system, etc.

  • When there are multiple if-else statements in the code, you can consider using the chain of responsibility pattern to reconstruct the code.

Features

  • Reduces the coupling between the sender and receiver of the request.

  • Disperse multiple conditional judgments into various processing classes to make the code clearer and the responsibilities clearer.

The chain of responsibility model also has certain shortcomings, such as:

  • All conditional judgments must be executed before the correct processing object is found. Again, when the chain of responsibility is too long, it may cause performance problems, which may result in a certain request not being processed.

Summary

  • The chain of responsibility reduces the coupling between the requester and the receiver, allowing multiple objects to have the opportunity to process a request . Makes the division of responsibilities more specific, helping to expand

Related recommendations:

Introduction to JavaScript responsibility chain model

Instance analysis of the chain of responsibility pattern in Java design patterns

The chain of responsibility pattern of commonly used design patterns and its PHP implementation

The above is the detailed content of Detailed explanation of js chain of responsibility model. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template