Web Developer and Author

code

Factory Design Pattern

function Factory(){
    var _this = this;
    var abstractionList = new Array();
    abstractionList.push({name: "one", obj: concreteOne});
    abstractionList.push({name: "two", obj: concreteTwo});
    abstractionList.push({name: "three", obj: concreteThree});
    
    abstractionList.forEach(function(abstraction){
        _this[abstraction.name] = new abstraction.obj();
    });
    
}
/**
*   CONCRETES 
*/
function concreteOne(){
    this.list = new Array()
}
concreteOne.prototype.add = function(data){
    this.list.push(data);
    console.log(this.list);
}
function concreteTwo(){
    this.list = new Array()
}
concreteTwo.prototype.add = function(data){
    this.list.push(data);
    console.log(this.list);
}
function concreteThree(){
    this.list = new Array()
}
concreteThree.prototype.add = function(data){
    this.list.push(data);
    console.log(this.list);
}
/**
*   EXAMPLE
*/
var f = new Factory()
f.one.add("add something to repo 1")
f.two.add("add something to repo 2")
f.three.add("add something to repo 3")