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

定义

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

实现

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

简单点的例子:

// 食品服务接口
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))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,329评论 19 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,204评论 25 709
  • 缓存在分布式系统中的应用 摘要 缓存是分布式系统中的重要组件,主要解决高并发,大数据场景下,热点数据访问的性能问题...
    garyond阅读 5,530评论 0 12
  • 天门寺旁边的石上,刻着“悟~心”两个字,我不知该不该将其读在一起,因为这两个字分别刻在两块石头上,而且“悟...
    时光清浅阿莲阅读 1,787评论 0 1
  • width:1000px;magin:0 auto;设置版心
    SmallTwo阅读 1,410评论 0 0