typescript: Prototype 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 | /** *file: prototypets.ts * Prototype Pattern 原型是一种创建型设计模式, 使你能够复制对象, 甚至是复杂对象, 而又无需使代码依赖它们所属的类。 * The example class that has cloning ability. We'll see how the values of field * with different types will be cloned. */ class Prototype { public primitive: any; public ducomponent: object; public circularReference: ComponentWithBackReference; public clone(): this { const clone = Object.create( this ); clone.component = Object.create( this .ducomponent); // Cloning an object that has a nested object with backreference // requires special treatment. After the cloning is completed, the // nested object should point to the cloned object, instead of the // original object. Spread operator can be handy for this case. clone.circularReference = { ... this .circularReference, prototype: { ... this }, }; return clone; } } /** * */ class ComponentWithBackReference { public prototype; constructor(prototype: Prototype) { this .prototype = prototype; } } /** * The client code. */ function clientCodeproto() { const p1 = new Prototype(); p1.primitive = 245; p1.ducomponent = new Date(); p1.circularReference = new ComponentWithBackReference(p1); let str= "" ; const p2 = p1.clone(); // if (p1.primitive == p2.primitive) { console.log('Primitive field values have been carried over to a clone. Yay! '); str=str+"Primitive ;"; } else { console.log(' Primitive field values have not been copied. Booo! '); str=str+"not been copied;"; } // if (p1.ducomponent == p2.ducomponent) { console.log(' Simple component has not been cloned. Booo! '); str=str+"Simple component not been cloned;"; } else { console.log(' Simple component has been cloned. Yay! '); str=str+"Simple component;"; } // if (p1.circularReference == p2.circularReference) { console.log(' Component with back reference has not been cloned. Booo! '); str=str+"Component not been cloned;"; } else { console.log(' Component with back reference has been cloned. Yay! '); str=str+"Component been cloned;"; } // if (p1.circularReference.prototype == p2.circularReference.prototype) { console.log(' Component with back reference is linked to original object. Booo! '); str=str+"Component to original object;"; } else { console.log(' Component with back reference is linked to the clone. Yay! '); str=str+" Component to the clone;"; } return str; } let strpro=clientCodeproto(); let strpro1="geovindu"; let messageprototype: string = ' Hello World,This is a typescript!,涂聚文 Geovin Du Web'; document.body.innerHTML = messageprototype+ "," +strpro+ "," +strpro1+ ",TypeScript 原型方法模式" |
调用:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <! 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:原型模式Prototype 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/prototypets.js"></ script > </ body > </ html > |
输出:
https://www.typescriptlang.org/docs/handbook/modules.html
https://www.typescriptlang.org/docs/handbook/classes.html
https://www.koderhq.com/tutorial/typescript/get-set/
https://dev.to/jmalvarez/builder-pattern-in-typescript-3on0
https://github.com/josemiguel-alvarez/design-patterns-typescript/
https://www.sourcecodeexamples.net/2020/08/typescript-prototype-pattern-example.html
https://www.tutorialspoint.com/typescript/typescript_object_prototype.htm
https://sbcode.net/typescript/prototype/
https://dev.to/takaakit/uml-diagram-for-gof-design-pattern-examples-in-typescript-46d5
https://www.dofactory.com/javascript/design-patterns/prototype
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(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-05 CSharp: Facade Pattern in donet core 3
2022-10-05 CSharp: Adapter Pattern in donet core 3
2022-10-05 CSharp: Decorator Pattern in donet core 3