Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created November 27, 2020 06:04
Show Gist options
  • Save misterpoloy/a6c99948becff15edc6f26dc9bf4eb13 to your computer and use it in GitHub Desktop.
Save misterpoloy/a6c99948becff15edc6f26dc9bf4eb13 to your computer and use it in GitHub Desktop.
Builder Pattern in JS
function Meal() {
this.make = function(builder){
builder.step1();
builder.step2();
builder.step3();
builder.step4();
return builder.get();
}
}
function MealBuilder(pattie,side,soda) {
this.meal = null;
this.step1 = function() {
this.meal = new Order();
};
this.step2 = function() {
this.meal.addBurger(pattie);
};
this.step3 = function(){
this.meal.addSide(side);
}
this.step4 = function(){
this.meal.addSoda(soda);
}
this.get = function() {
return this.meal;
};
}
function Order() {
this.burger = null;
this.side = null;
this.soda = null;
this.addBurger = function(pattie) {
this.burger = pattie;
};
this.addSide = function(side) {
this.side = side;
};
this.addSoda = function(soda){
this.soda = soda;
}
this.display = function(){
console.log(`You meal has a ${this.burger} burger, ${this.side} on the side, and a ${this.soda}.`)
}
}
var meal = new Meal();
var mealBuilder = new MealBuilder("chicken","curly fries","coke");
var chickenBurgerMeal = meal.make(mealBuilder);
chickenBurgerMeal.display();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment