typescript: Chain of Responsibility Pattern
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | /** * 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 责任链模式" ; |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <! 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | { "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(涂聚文)
分类:
Ajax&JavaScript
标签:
desgin patterns
, 設計模式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
2022-10-11 CSharp: Mediator Pattern in donet core 3
2018-10-11 SQL Server: Datetime,Datetime2
2016-10-11 MySQL 5.7 create VIEW or FUNCTION or PROCEDURE