typescript: Chain of Responsibility Pattern

发布时间 2023-10-11 08:59:50作者: ®Geovin Du Dream Park™

 

/**
 * Chain of Responsibility Pattern 责任链是一种行为设计模式, 允许你将请求沿着处理者链进行发送, 直至其中一个处理者对其进行处理。
 * file: Chaints.ts
 * The Handler interface declares a method for building the chain of handlers.
 * It also declares a method for executing a request.
 */
interface Handler {
    /**
     * 
     * @param handler 
     */
    setNext(handler: Handler): Handler;
    /**
     * 
     * @param request 
     */
    handle(request: string): string;
}

/**
 * The default chaining behavior can be implemented inside a base handler class.
 */
abstract class AbstractHandler implements Handler
{
    /**
     * 
     */
    private nextHandler: Handler;
    /**
     * 
     * @param handler 
     * @returns 
     */
    public setNext(handler: Handler): Handler {
        this.nextHandler = handler;
        // Returning a handler from here will let us link handlers in a
        // convenient way like this:
        // monkey.setNext(squirrel).setNext(dog);
        return handler;
    }
    /**
     * 
     * @param request 
     * @returns 
     */
    public handle(request: string): string {
        if (this.nextHandler) {
            return this.nextHandler.handle(request);
        }

        return null;
    }
}

/**
 * All Concrete Handlers either handle a request or pass it to the next handler
 * in the chain.
 */
class MonkeyHandler extends AbstractHandler {
    /**
     * 
     * @param request 
     * @returns 
     */
    public handle(request: string): string {
        if (request === 'Banana') {
            return `Monkey: I'll eat the ${request}.`;
        }
        return super.handle(request);

    }
}

/**
 * 
 */
class SquirrelHandler extends AbstractHandler {
    /**
     * 
     * @param request 
     * @returns 
     */
    public handle(request: string): string {
        if (request === 'Nut') {
            return `Squirrel: I'll eat the ${request}.`;
        }
        return super.handle(request);
    }
}
/**
 * 
 * 
 */
class DogHandler extends AbstractHandler {

    /**
     * 
     * @param request 
     * @returns 
     */
    public handle(request: string): string {
        if (request === 'MeatBall') {
            return `Dog: I'll eat the ${request}.`;
        }
        return super.handle(request);
    }
}

/**
 * The client code is usually suited to work with a single handler. In most
 * cases, it is not even aware that the handler is part of a chain.
 */
function clientCodeChain(handler: Handler) {

    const foods = ['Nut', 'Banana', 'Cup of coffee'];
    let str="";
    for (const food of foods) {
        console.log(`Client: Who wants a ${food}?`);

        const result = handler.handle(food);
        if (result) {
            console.log(`  ${result}`);
            str=str+","+result;
        } else {
            console.log(`  ${food} was left untouched.`);
            str=str+","+food;
        }
    }
    return str;
}


let pubch1="";
let pubch2="";
let pubch3="Geovin Du";
let pubch4="geovindu";
/**
 * The other part of the client code constructs the actual chain.
 */
const monkey = new MonkeyHandler();
const squirrel = new SquirrelHandler();
const dog = new DogHandler();

monkey.setNext(squirrel).setNext(dog);

/**
 * The client should be able to send a request to any handler, not just the
 * first one in the chain.
 */
console.log('Chain: Monkey > Squirrel > Dog\n');
pubch1=clientCodeChain(monkey);
console.log('');

console.log('Subchain: Squirrel > Dog\n');
pubch2=clientCodeChain(squirrel);

let messageChain: string = 'Hello World,This is a typescript!,涂聚文 Geovin Du.Web';
document.body.innerHTML = messageChain+",<br/>one="+pubch1+",<br/>two="+pubch2+",<br/>three="+pubch3+",<br/>four="+pubch4+",<br/>TypeScript Chain of Responsibility Pattern 责任链模式";

  

调用:

<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <head><title>TypeScript Hello Chain of Responsibility Pattern 责任链模式</title>
      <meta name="Description" content="geovindu,涂聚文,Geovin Du"/>
<meta name="Keywords" content="geovindu,涂聚文,Geovin Du"/>
<meta name="author" content="geovindu,涂聚文,Geovin Du"/> 
    </head>
    <body>
        <script src="dist/Chaints.js"></script>
    </body>
</html>

  

 

输出: