📜  外观设计模式打字稿代码示例

📅  最后修改于: 2022-03-11 14:48:18.030000             🧑  作者: Mango

代码示例1
interface Store {
  product(name: string): string
  category(): string
}

class Minuman implements Store {
   product(name: string): string {
      return name
   }
   
   category(): string {
      return 'minuman'
   }
}

class Makanan implements Store {   
   product(name: string): string {
      return name
   }
   
   category(): string {
     return 'makanan'
   }
}

class StoreFaced {
  public minuman: InstanceType
  public makanan: InstanceType
  
  constructor() {
    this.minuman = new Minuman()
    this.makanan = new Makanan()
  }
  
  storeMinuman(name: string): string {
    return this.minuman.product(name)
  }
  
  storeMakanan(name: string): string {
    return this.makanan.product(name)
  }
}

const res = new StoreFaced()
console.log(`product name: ${res.storeMinuman("cola cola")} and product category ${res.minuman.category()}`)
console.log(`product name: ${res.storeMakanan("biskuat")} and product category ${res.makanan.category()}`)