sdfxs

Untitled

May 11th, 2021
608
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Car {
  2.     parts: string[] = []
  3. }
  4.  
  5. interface ICarBuilder {
  6.     addWheels(): this
  7.     addEngine(): this
  8.     getResult(): Car
  9. }
  10.  
  11. class SportCarBuilder implements ICarBuilder {
  12.     product: Car
  13.  
  14.     constructor() {
  15.         this.product = new Car()
  16.     }
  17.  
  18.     addWheels() {
  19.         this.product.parts.push('Колёса')
  20.         return this
  21.     }
  22.  
  23.     addEngine() {
  24.         this.product.parts.push('Мотор')
  25.         return this
  26.     }
  27.  
  28.     getResult() {
  29.         return this.product
  30.     }
  31. }
  32.  
  33. class MiniCarBuilder implements ICarBuilder {
  34.     product: Car
  35.  
  36.     constructor() {
  37.         this.product = new Car()
  38.     }
  39.  
  40.     addWheels() {
  41.         this.product.parts.push('колёсики')
  42.         return this
  43.     }
  44.  
  45.     addEngine() {
  46.         this.product.parts.push('моторчик')
  47.         return this
  48.     }
  49.  
  50.     getResult() {
  51.         return this.product
  52.     }
  53. }
  54.  
  55. class Director {
  56.  
  57.     static construct(builder: ICarBuilder) {
  58.         return builder
  59.             .addWheels()
  60.             .addEngine()
  61.             .getResult()
  62.     }
  63. }
  64.  
  65. const product1 = Director.construct(new SportCarBuilder())
  66. console.log(product1.parts)
  67.  
  68. const product2 = Director.construct(new MiniCarBuilder())
  69. console.log(product2.parts)
RAW Paste Data