设计模式
ts实现设计模式
LetsStudy
The growth of age will not be a hindrance to study.
展开
-
1.3 发布订阅模式
通过发布订阅模式实现异步接口的顺序调用publish.jsclass Publish{ constructor() { this.events = {} } sub(event,callback){ if(this.events[event]){ this.events[event].push(callback) } else this.events[event]=[callback] } publish(event,.原创 2022-01-27 16:41:15 · 688 阅读 · 0 评论 -
1.2 观察者模式
1、监听某个变量的值,当这个变量的值变化时,触发某个方法oberver.js//观察者class Observers { constructor(name) { this.name = name; } update(target_value){ console.log(this.name + ':a的值改变了,当前值为:' + target_value) }}exports = module.exports = Observerstarget.js原创 2022-01-26 16:57:12 · 350 阅读 · 0 评论 -
1.1 ts工厂模式实现
factoryinterface.tsnamespace AnimalInterface{ export interface Animal { say(): void } export interface AnimalFactory { getInstance(type: string): Animal; }}factoryimp.ts/// <reference path="interfacefactory.ts".原创 2021-07-26 17:41:20 · 365 阅读 · 0 评论