|
| 1 | +class House { |
| 2 | + private walls: number; |
| 3 | + private doors: number; |
| 4 | + private windows: number; |
| 5 | + private roof: string; |
| 6 | + private swimmingPool: boolean; |
| 7 | + |
| 8 | + setWalls(walls: number) { |
| 9 | + this.walls = walls; |
| 10 | + } |
| 11 | + |
| 12 | + setDoors(doors: number) { |
| 13 | + this.doors = doors; |
| 14 | + } |
| 15 | + |
| 16 | + setWindows(windows: number) { |
| 17 | + this.windows = windows; |
| 18 | + } |
| 19 | + |
| 20 | + setRoof(roof: string) { |
| 21 | + this.roof = roof; |
| 22 | + } |
| 23 | + |
| 24 | + setSwimmingPool(swimmingPool: boolean) { |
| 25 | + this.swimmingPool = swimmingPool; |
| 26 | + } |
| 27 | + |
| 28 | + describeHouse() { |
| 29 | + console.log("House description:"); |
| 30 | + console.log("Walls: ", this.walls); |
| 31 | + console.log("Doors: ", this.doors); |
| 32 | + console.log("Windows: ", this.windows); |
| 33 | + console.log("Roof: ", this.roof); |
| 34 | + console.log("Swimming pool: ", this.swimmingPool); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +interface HouseBuilder { |
| 39 | + buildWalls(): void; |
| 40 | + buildDoors(): void; |
| 41 | + buildWindows(): void; |
| 42 | + buildRoof(): void; |
| 43 | + buildSwimmingPool(): void; |
| 44 | + getResult(): House; |
| 45 | +} |
| 46 | + |
| 47 | +class SmallHouseBuilder implements HouseBuilder { |
| 48 | + private house: House; |
| 49 | + |
| 50 | + constructor() { |
| 51 | + this.house = new House(); |
| 52 | + } |
| 53 | + |
| 54 | + buildWalls() { |
| 55 | + this.house.setWalls(4); |
| 56 | + } |
| 57 | + |
| 58 | + buildDoors() { |
| 59 | + this.house.setDoors(1); |
| 60 | + } |
| 61 | + |
| 62 | + buildWindows() { |
| 63 | + this.house.setWindows(1); |
| 64 | + } |
| 65 | + |
| 66 | + buildRoof() { |
| 67 | + this.house.setRoof("wooden roof"); |
| 68 | + } |
| 69 | + |
| 70 | + buildSwimmingPool() { |
| 71 | + this.house.setSwimmingPool(false); |
| 72 | + } |
| 73 | + |
| 74 | + getResult() { |
| 75 | + return this.house; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +class LargeHouseBuilder implements HouseBuilder { |
| 80 | + private house: House; |
| 81 | + |
| 82 | + constructor() { |
| 83 | + this.house = new House(); |
| 84 | + } |
| 85 | + |
| 86 | + buildWalls() { |
| 87 | + this.house.setWalls(12); |
| 88 | + } |
| 89 | + |
| 90 | + buildDoors() { |
| 91 | + this.house.setDoors(5); |
| 92 | + } |
| 93 | + |
| 94 | + buildWindows() { |
| 95 | + this.house.setWindows(4); |
| 96 | + } |
| 97 | + |
| 98 | + buildRoof() { |
| 99 | + this.house.setRoof("concrete roof"); |
| 100 | + } |
| 101 | + |
| 102 | + buildSwimmingPool() { |
| 103 | + this.house.setSwimmingPool(true); |
| 104 | + } |
| 105 | + |
| 106 | + getResult() { |
| 107 | + return this.house; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +class HouseDirector { |
| 112 | + private builder: HouseBuilder; |
| 113 | + |
| 114 | + constructor(builder: HouseBuilder) { |
| 115 | + this.builder = builder; |
| 116 | + } |
| 117 | + |
| 118 | + constructHouse() { |
| 119 | + this.builder.buildWalls(); |
| 120 | + this.builder.buildDoors(); |
| 121 | + this.builder.buildWindows(); |
| 122 | + this.builder.buildRoof(); |
| 123 | + this.builder.buildSwimmingPool(); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +const smallBuilder = new SmallHouseBuilder(); |
| 128 | +const largeBuilder = new LargeHouseBuilder(); |
| 129 | + |
| 130 | +const director1 = new HouseDirector(smallBuilder); |
| 131 | +director1.constructHouse(); |
| 132 | +const smallHouse = smallBuilder.getResult(); |
| 133 | +smallHouse.describeHouse(); |
| 134 | + |
| 135 | +const director2 = new HouseDirector(largeBuilder); |
| 136 | +director2.constructHouse(); |
| 137 | +const largeHouse = largeBuilder.getResult(); |
| 138 | +largeHouse.describeHouse(); |
0 commit comments