Chain of Responsibility model

Responsibility chain pattern is a behavioral design pattern.

Behavioral patterns are used to describe interactions between classes or objects and the assignment of responsibilities

The chain of responsibility is defined layer by layer and handled step by step until the task is completed

Graph LR initiates a request --> processor 1 -->... --> Processor n --> Done

Application scenarios

Take a requirement like this

Remove numbers and letters from a paragraph of text

Start by defining two filters

function handleA(str){
    return str.replace(/\d/g.' ')}function handleA(str){
    return str.replace(/[a-zA-Z]/g.' ')}function handle(str){
    str = handleA(str)
    return handleB(str)
}
Copy the code

Perform processing

let str = "1A Middle B2 Country C3 people 4D";
handle(str) / / the Chinese
Copy the code