设计模式在 TypeScript 中的应用
来源:互联网 发布:淘宝订单要不要清洗 编辑:程序博客网 时间:2024/06/07 07:36
定义
代理模式是为一个对象提供一个代用品,或占位符,以便控制对它的访问。
实现
思路:把客户端真正调用的类和方法隐藏,只暴露代理类给客户端。
简单点的例子:
// 食品服务接口interface FootService { makeChicken (salt: string): void; makeNoodle (salt: string): void}// 食物接口class Foot { // 种类 public type: string // 重量 public salt: string public constructor (type: string, salt: string) { this.type = type this.salt = salt this.cook() } // cook public cook (): void { console.log(`种类:${this.type},重量:${this.salt}`) }}// 真实的食品服务class FootServiceReal implements FootService { public chicken: Foot public Noodle: Foot public makeChicken (salt: string): any { this.chicken = new Foot('chicken', salt) } public makeNoodle (salt: string): any { this.Noodle = new Foot('noodle', salt) }}// 代理食品服务class FootServiceProxy implements FootService { // 真实的实现类 private footServiceReal: FootServiceReal private prepareFood () { if (!this.footServiceReal) { this.footServiceReal = new FootServiceReal() } } public makeChicken () { console.log('马上开始做鸡肉') console.log('==========') this.prepareFood() this.footServiceReal.makeChicken('500g') console.log('==========') console.log('鸡肉做好了') } public makeNoodle () { console.log('马上开始做面条') console.log('==========') this.prepareFood() this.footServiceReal.makeNoodle('100g') console.log('==========') console.log('面条做好了') }}const foot = new FootServiceProxy()foot.makeChicken()console.log('========')foot.makeNoodle()
缓存代理比较常见,可以为一些开销比较大的运算结果提供缓存,在下次运算时,如果传递进来的参数跟以前一致,则可以直接返回前面存储的运算结果。
// 加法function add (arg: Array<number>): number { console.log('进行一次加法计算') return arg.reduce((prev: number, cur: number): number => prev + cur, 0)}// 乘法function mul (arg: Array<number>): number { console.log('进行一次乘法计算') return arg.reduce((prev: number, cur: number): number => prev * cur, 1)}// 代理class CalculateProxy { private cache: Object = {} private fn: Function public constructor (fn: Function) { this.fn = fn } public calculate (...arg: Array<number>): void { const str: string = arg.join(',') if (str in this.cache) { return this.cache[str] } else { return this.cache[str] = this.fn(arg) } }}const addCalculateProxy = new CalculateProxy(add)console.log(addCalculateProxy.calculate(1, 2, 3, 4))console.log(addCalculateProxy.calculate(1, 2, 3, 4))console.log('=========')const mulCalculateProxy = new CalculateProxy(mul)console.log(mulCalculateProxy.calculate(1, 2, 3, 4))console.log(mulCalculateProxy.calculate(1, 2, 3, 4))
阅读全文
0 0
- 设计模式在 TypeScript 中的应用
- 设计模式在 TypeScript 中的应用
- 设计模式在 TypeScript 中的应用
- 设计模式在 TypeScript 中的应用
- 设计模式在EJB中的应用
- 设计模式在鼠标绘图中的应用
- 设计模式在项目中的应用
- 设计模式在实际项目中的应用
- 设计模式在EJB中的应用
- 设计模式在EJB中的应用
- 设计模式在EJB中的应用
- builder模式在游戏设计中的应用
- 设计模式在GIS开发中的应用
- 设计模式在EJB中的应用
- 设计模式在游戏开发中的应用
- 设计模式在EJB中的应用
- 设计模式在EJB中的应用
- 设计模式在Eclipse平台中的应用
- 微信app支付java后台流程、原理分析及nei网穿透 转载自(http://www.cnblogs.com/MrRightZhao/p/7930916.html)
- PYTHON 实现 NBA 赛程查询工具(一)—— pyqt界面
- 【血泪控诉】海南杏林肛肠医院搞坏了我的肛门
- webdriver——键盘操作
- 洛谷1108 低价购买
- 设计模式在 TypeScript 中的应用
- 百度统计跳转到空白页BUG处理
- Android M Launcher3主流程源码浅析
- CodeForces 7 C.Line(一元线性同余方程)
- SDUT ACM 实验九 C语言实验——大小写转换
- python MySQL 批量插入
- 输入几个数求平均值
- 百度起诉前高管:景驰科技创始人王劲 索赔5000万!
- SDUT ACM 实验九 大小写转换