Skip to content

Instantly share code, notes, and snippets.

@misterpoloy
Created November 27, 2020 06:52
Show Gist options
  • Save misterpoloy/ab3e33e48d4b3dd46f1eaf6b7946235b to your computer and use it in GitHub Desktop.
Save misterpoloy/ab3e33e48d4b3dd46f1eaf6b7946235b to your computer and use it in GitHub Desktop.
Abstract Pattern
function Soda(name,type,price) {
this.name = name;
this.type = type;
this.price = price;
this.display = function(){
console.log(`The ${this.type} ${this.name} costs ${this.price} dollars`)
}
}
function Chips(name,type,price) {
this.name = name;
this.type = type;
this.price = price;
this.display = function(){
console.log(`The ${this.type} ${this.name} costs ${this.price} dollars`)
}
}
function JunkFoodFactory(){
var junkfood;
this.createJunkFood = function(name,type,price) {
switch (name) {
case "chips":
junkfood = new Chips(name,type,price);
break;
case "soda":
junkfood = new Soda(name,type,price);
break;
default:
junkfood = new Chips(name,type,price);
break;
}
return junkfood;
}
}
var factory = new JunkFoodFactory();
var chips = factory.createJunkFood("chips","potato",1.50)
chips.display()
chips = factory.createJunkFood("chips","corn",2.50)
chips.display()
var soda = factory.createJunkFood("soda", "Energy Drink", 10)
soda.display()
soda = factory.createJunkFood("soda", "Cola", 7)
soda.display()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment