JavaScript Builder Pattern Example
JavaScript
function ActionFactory(options){
if(options.type === 'video'){
return new VideoAction().build(options);
}
};
function Action() {
var _instance;
var _Action = function(options) {
this.type = options.type;
this.value = options.value;
};
this.setType = function(type){
_instance.type = type;;
return this;
};
this.getType = function() {
return _instance.type;
};
this.build = function(options) {
_instance = new _Action(options);
return _instance;
}
};
Action.prototype.doSomething = function(){
console.log('doing something common');
console.log(this.value);
};
function VideoAction (){
Action.call(this);
var unCommonFunc = function(){
Action.doSomething.call(this);
};
};
var actionChain = [];
actionChain.push(ActionFactory({type: 'video', value: 'someValue'}));
console.log(actionChain[0].getType());
console.log(actionChain[0].setType('newVideoType').getType())