Last active
February 5, 2019 14:07
-
-
Save rajikaimal/e8bfd3da5de0f9cc4c709bc643511c4a to your computer and use it in GitHub Desktop.
Abstract factory in JavaScript
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 porscheCar(params) { | |
this.engine = params.engine; | |
this.tyreModel = params.tyreModel; | |
this.gears = params.gears; | |
} | |
function porscheCarFactory() { | |
this.createCar = function(params) { | |
return new porscheCar(params); | |
} | |
} | |
function ferrariCar(params) { | |
this.engine = params.engine; | |
this.tyreModel = params.tyreModel; | |
this.gears = params.gears; | |
this.turbo = params.turbo; | |
} | |
function ferrariCarFactory() { | |
this.createCar = function(params) { | |
return new ferrariCar(params); | |
} | |
} | |
function carFactoryImpl() { | |
this.porcheCarFactory = new porscheCarFactory(); | |
this.ferrariCar = new ferrariCarFactory(); | |
this.getPorsche = function() { | |
return this.porcheCarFactory; | |
} | |
this.getFerrari = function() { | |
return this.ferrariCar; | |
} | |
} | |
var fact = new carFactoryImpl(); | |
var params = { engine: "", tyreModel: "", gears: null }; | |
var concretePorsche = fact.getPorsche().createCar(params); | |
console.log(concretePorsche.engine); | |
params = { engine: "", tyreModel: "", gears: null, turbo: true }; | |
var concreteFerrari = fact.getFerrari().createCar(params); | |
console.log(concreteFerrari.turbo); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment