Created
February 18, 2019 14:11
-
-
Save AndriiShtoiko/409a57837cf15dc715816a24f358a464 to your computer and use it in GitHub Desktop.
Common example for builder patter
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 BigMacMenu { | |
constructor(build) { | |
this.menuName = build.menuName; | |
this.drink = build.drink; | |
this.mainDish = build.mainDish; | |
this.sideDish = build.sideDish; | |
} | |
static get Builder() { | |
class Builder { | |
constructor(menuName) { | |
this.menuName = menuName; | |
} | |
addDrink(drink) { | |
this.drink = drink; | |
return this; | |
} | |
addMainDish(mainDish) { | |
this.mainDish = mainDish; | |
return this; | |
} | |
addSideDish(sideDish) { | |
this.sideDish = sideDish; | |
return this; | |
} | |
build() { | |
return new BigMacMenu(this); | |
} | |
} | |
return Builder; | |
} | |
} | |
let menu = new BigMacMenu.Builder(“Menu with juice”) | |
.addDrink(“Cola”) | |
.addMainDish(“Big Mac”) | |
.build(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment