sdfxs

Untitled

May 11th, 2021
638
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. interface Beverage {
  2.     getDescription(): string;
  3. }
  4.  
  5. class Coffee implements Beverage {
  6.     public getDescription(): string {
  7.         return 'Espresso';
  8.     }
  9. }
  10.  
  11. class Tea implements Beverage {
  12.     public getDescription(): string {
  13.         return 'Black Tea';
  14.     }
  15. }
  16.  
  17. class WithMilk implements Beverage {
  18.     private component: Beverage;
  19.  
  20.     constructor(component: Beverage) {
  21.         this.component = component;
  22.     }
  23.  
  24.     public getDescription(): string {
  25.         return `${this.component.getDescription()} & milk`;
  26.     }
  27. }
  28.  
  29. class WithSugar implements Beverage {
  30.     private component: Beverage;
  31.  
  32.     constructor(component: Beverage) {
  33.         this.component = component;
  34.     }
  35.  
  36.     public getDescription(): string {
  37.         return `${this.component.getDescription()} & sugar`;
  38.     }
  39. }
  40.  
  41. function clientCode(component: Beverage) {
  42.     console.log(`RESULT: ${component.getDescription()}`);
  43. }
  44.  
  45.  
  46. let coffee = new Coffee();
  47. console.log('До оборачивания в декратор:');
  48. console.log(coffee.getDescription());
  49.  
  50. coffee = new WithSugar(new WithMilk(coffee));
  51. console.log('После:');
  52. console.log(coffee.getDescription());
RAW Paste Data