Created
November 27, 2020 06:04
-
-
Save misterpoloy/a6c99948becff15edc6f26dc9bf4eb13 to your computer and use it in GitHub Desktop.
Builder Pattern in JS
This file contains hidden or 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
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