Skip to content

Instantly share code, notes, and snippets.

@AndriiShtoiko
Created February 18, 2019 14:11
Show Gist options
  • Save AndriiShtoiko/409a57837cf15dc715816a24f358a464 to your computer and use it in GitHub Desktop.
Save AndriiShtoiko/409a57837cf15dc715816a24f358a464 to your computer and use it in GitHub Desktop.
Common example for builder patter
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