Design: Builder Pattern Implementation
Design and implement the builder pattern for creating complex objects in a low-level design context. Out of scope: integration with UI or persistence layers.
Functional Requirements
Non-Functional Requirements
Client --> Builder Interface --> Concrete Builder --> Product
\ /
--------> Director (optional) <---Builder pattern in system design?this) so calls can be chained.this (the builder instance) to allow chaining -> Option Dclass House {
constructor(walls, doors) {
this.walls = walls;
this.doors = doors;
}
describe() {
return `House with ${this.walls} walls and ${this.doors} doors.`;
}
}
class HouseBuilder {
constructor() {
this.walls = 0;
this.doors = 0;
}
setWalls(count) {
this.walls = count;
return this;
}
setDoors(count) {
this.doors = count;
return this;
}
build() {
return new House(this.walls, this.doors);
}
}
const house = new HouseBuilder().setWalls(4).setDoors(2).build();
console.log(house.describe());class CarBuilder {
constructor() {
this.color = '';
}
setColor(color) {
this.color = color;
}
build() {
return { color: this.color };
}
}
const car = new CarBuilder().setColor('red').build();
console.log(car.color);this, so chaining to build() fails.Pass the test and this pattern moves unknown