Created
August 3, 2020 09:53
-
-
Save yaakov123/8b94b27726f34d9a7de508bb45b707ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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