Skip to content

Instantly share code, notes, and snippets.

@yaakov123
Created August 3, 2020 09:53
Show Gist options
  • Save yaakov123/8b94b27726f34d9a7de508bb45b707ed to your computer and use it in GitHub Desktop.
Save yaakov123/8b94b27726f34d9a7de508bb45b707ed to your computer and use it in GitHub Desktop.
class House {
constructor() {
this.walls = null;
this.roof = null;
this.pool = null;
this.garden = null;
this.internet = null;
}
setWalls(walls) {
this.walls = walls;
}
setRoof(roof) {
this.roof = roof;
}
setPool(pool) {
this.pool = pool;
}
setGarden(garden) {
this.garden = garden;
}
setInternet(internet) {
this.internet = internet;
}
}
class HouseBuilder {
constructor() {
this.house = new House();
}
buildWalls(walls) {
this.house.setWalls(walls);
return this; // returning this for method chaining
}
buildRoof(roof) {
this.house.setRoof(roof);
return this;
}
buildPool(pool) {
this.house.setPool(pool);
return this;
}
buildGarden(garden) {
this.house.setGarden(garden);
return this;
}
getHouse() {
const house = this.house;
this.reset();
return house;
}
reset() {
this.house = new House();
}
}
const house = builder
.buildWalls(4)
.buildRoof('sloped')
.buildGarden(['trees', 'grass', 'peacocks'])
.getHouse();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment