Created
February 18, 2019 14:11
-
-
Save AndriiShtoiko/fe919c52d4c733e1cdad7ed5dd9f5584 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