美文网首页TypeScript程序员我爱编程
设计模式在 TypeScript 中的应用 - 代理模式

设计模式在 TypeScript 中的应用 - 代理模式

作者: 三毛丶 | 来源:发表于2017-12-23 10:42 被阅读49次

定义

代理模式是为一个对象提供一个代用品,或占位符,以便控制对它的访问。

实现

思路:把客户端真正调用的类和方法隐藏,只暴露代理类给客户端。

简单点的例子:

// 食品服务接口
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))

相关文章

  • 设计模式在 TypeScript 中的应用 - 代理模式

    定义 代理模式是为一个对象提供一个代用品,或占位符,以便控制对它的访问。 实现 思路:把客户端真正调用的类和方法隐...

  • Typescript 代理模式(Proxy)

    标签: 前端 设计模式 代理模式 typescript proxy 请仔细阅读下面代码,理解其中的设计理念。 代理...

  • 设计模式在 TypeScript 中的应用 - 策略模式

    定义 定义一系列的算法,把它们一个个封装起来,并且使它们可以相互替换。 实现 思路:创建表示各种策略的对象,和一个...

  • C++ 设计模式 —— 12.代理模式

    代理模式:一种结构型设计模式 应用场景:代理模式,工作中很常用。我们在公司使用网络服务时,都会经过代理服务器,这个...

  • 通俗易懂的讲解一下Java的代理模式

    在我们通常的应用中,代理模式也是我们常用的设计模式之一。所谓的代理模式是指客户端并不直接调用实际的对象,而是通过调...

  • 设计模式(六)代理模式

    1.代理模式简介 代理模式介绍 代理模式也叫委托模式,是结构型设计模式的一种。在现实生活中我们用到类似代理模式的场...

  • 设计模式在 TypeScript 中的应用 - 单例模式

    定义 只有一个实例,并提供全局访问。 实现 思路:用一个变量来标识当前是否已经为某个类创建过对象,如果是,则在下一...

  • spring框架中的设计模式二

    在这篇文章中,介绍4种设计模式。结构型设计模式:代理和复合模式。行为型设计模式:策略和模板方法模式。 代理模式 面...

  • Delegate的基本使用

    代理的基本使用 代理是一种通用的设计模式,在iOS中对代理设计模式支持的很好,有特定的语法来实现代理模式,OC语言...

  • iOS之代理笔记

    代理的基本使用 代理是一种通用的设计模式,在iOS中对代理设计模式支持的很好,有特定的语法来 实现代理模式,OC语...

网友评论

    本文标题:设计模式在 TypeScript 中的应用 - 代理模式

    本文链接:https://www.haomeiwen.com/subject/neifgxtx.html