typescript: Chain of Responsibility Pattern
/**
* 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>
输出:

tsconfig.json:
{
"compilerOptions":
{
"target": "ES2015",
"module": "CommonJS",
"outDir": "./dist",
"rootDir": "./src",
"resolveJsonModule": true,
"composite": true, // required on the dependency project for references to work https://github.com/microsoft/TypeScript/issues/30693
"sourceMap": true,
/*
"strict": true,
"experimentalDecorators": true,
"useDefineForClassFields": false,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"esModuleInterop": true,
"moduleResolution": "node",
// Tells TypeScript to read JS files, as normally they are ignored as source files
"allowJs": true,
// Generate d.ts files
"declaration": true,
// go to js file when using IDE functions like "Go to Definition" in VSCode
"declarationMap": true,
// This compiler run should only output d.ts files
"emitDeclarationOnly": true*/
"baseUrl": ".",
"paths": {
"dotenv": ["./dist/du.d.ts"]
},
},
"includes": [
"src/**/*.ts",
"other-src/**/*.ts"
],
"exclude":[
"rollup.config.js",
"test",
"dist",
"node_modules",
],
}
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)生存.---Geovin Du(涂聚文)
浙公网安备 33010602011771号