|
| 1 | +export {}; |
| 2 | + |
| 3 | +interface Builder { |
| 4 | + producePartA(): void; |
| 5 | + producePartB(): void; |
| 6 | + producePartC(): void; |
| 7 | +} |
| 8 | + |
| 9 | +class ConcreteBuilder1 implements Builder { |
| 10 | + private product: Product1; |
| 11 | + |
| 12 | + constructor() { |
| 13 | + this.product = new Product1(); |
| 14 | + } |
| 15 | + |
| 16 | + public reset(): void { |
| 17 | + this.product = new Product1(); |
| 18 | + } |
| 19 | + |
| 20 | + public producePartA(): void { |
| 21 | + this.product.parts.push('PartA1'); |
| 22 | + } |
| 23 | + |
| 24 | + public producePartB(): void { |
| 25 | + this.product.parts.push('PartB1'); |
| 26 | + } |
| 27 | + |
| 28 | + public producePartC(): void { |
| 29 | + this.product.parts.push('PartC1'); |
| 30 | + } |
| 31 | + |
| 32 | + public getProduct(): Product1 { |
| 33 | + const result = this.product; |
| 34 | + this.reset(); |
| 35 | + return result; |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class Product1 { |
| 40 | + public parts: string[] = []; |
| 41 | + |
| 42 | + public listParts(): void { |
| 43 | + console.log(`Product parts: ${this.parts.join(', ')}\n`); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +class Director { |
| 48 | + private builder: Builder; |
| 49 | + |
| 50 | + constructor(builder: Builder) { |
| 51 | + this.builder = builder; |
| 52 | + } |
| 53 | + |
| 54 | + public buildMinimalViableProduct(): void { |
| 55 | + this.builder.producePartA(); |
| 56 | + } |
| 57 | + |
| 58 | + public buildFullFeaturedProduct(): void { |
| 59 | + this.builder.producePartA(); |
| 60 | + this.builder.producePartB(); |
| 61 | + this.builder.producePartC(); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function clientCode() { |
| 66 | + const builder = new ConcreteBuilder1(); |
| 67 | + const director = new Director(builder); |
| 68 | + |
| 69 | + console.log('Standard basic product:'); |
| 70 | + director.buildMinimalViableProduct(); |
| 71 | + builder.getProduct().listParts(); |
| 72 | + |
| 73 | + console.log('Standard full featured product:'); |
| 74 | + director.buildFullFeaturedProduct(); |
| 75 | + builder.getProduct().listParts(); |
| 76 | + |
| 77 | + console.log('Custom product:'); |
| 78 | + builder.producePartA(); |
| 79 | + builder.producePartC(); |
| 80 | + builder.getProduct().listParts(); |
| 81 | +} |
| 82 | + |
| 83 | +clientCode(); |
0 commit comments