Permalink
Browse files

factory pattern

  • Loading branch information...
erichelin committed Apr 27, 2017
1 parent 3e18458 commit 8fc4145436cfd90d7c7fcd0d9b6294826082743c
@@ -0,0 +1,8 @@
var AbsFactory = {
name: undefined,
createObject: function() {
console.log('AbsFactory -> createObject() - should not be called')
},
};
module.exports.AbsFactory = AbsFactory;
@@ -0,0 +1,5 @@
var Object = {
name: undefined,
};
module.exports.Object = Object;
@@ -0,0 +1,32 @@
var absObjFactoryRef = require('./AbsFactory');
var absObjRef = require('./AbsObject');
function demoFactoryPattern() {
console.log('inside demoFactoryPattern');
var names = ['A', 'B', 'C'];
names.forEach((name) => {
console.log('running factory for ' + name);
var factory = buildFactory(name);
factory.createObject();
});
};
function buildFactory(name){
var factory = Object.create(absObjFactoryRef.AbsFactory);
factory.name = 'Object' + name + 'Factory';
factory.createObject = function() {
console.log(this.name + ' -> createObject() -> Object' + name + ' created');
var obj = Object.create(absObjRef.Object);
obj.name = 'Object' + name;
return obj;
};
return factory;
}
module.exports.demoFactoryPattern = demoFactoryPattern;
@@ -2,48 +2,55 @@ var express = require('express');
var app = module.exports = express();
app.get('/javascript/patterns/command', function(req, res){
var cmd = require('./command/commandDriver');
cmd.demoCommandPattern();
var ref = require('./command/commandDriver');
ref.demoCommandPattern();
res.send('/javascript/patterns/command - complete!');
});
app.get('/javascript/patterns/visitor', function(req, res){
var cmd = require('./visitor/visitorDriver');
cmd.demoVisitorPattern();
var ref = require('./visitor/visitorDriver');
ref.demoVisitorPattern();
res.send('/javascript/patterns/visitor - complete!');
});
app.get('/javascript/patterns/observer', function(req, res){
var cmd = require('./observer/observerDriver');
cmd.demoObserverPattern();
var ref = require('./observer/observerDriver');
ref.demoObserverPattern();
res.send('/javascript/patterns/observer - complete!');
});
app.get('/javascript/patterns/serviceLocator', function(req, res){
var cmd = require('./dependancyLocator/dependancyLocatorDriver');
cmd.demoServiceLocatorPattern();
var ref = require('./dependancyLocator/dependancyLocatorDriver');
ref.demoServiceLocatorPattern();
res.send('/javascript/patterns/serviceLocator - complete!');
});
app.get('/javascript/patterns/singleton', function(req, res){
var cmd = require('./singleton/singletonDriver');
cmd.demoSingletonPattern();
var ref = require('./singleton/singletonDriver');
ref.demoSingletonPattern();
res.send('/javascript/patterns/singleton - complete!');
});
app.get('/javascript/patterns/chainOfResponsibility', function(req, res){
var cmd = require('./chainOfResponsibility/chainOfResponsibilityDriver');
cmd.demoChainOfResponsibility();
var ref = require('./chainOfResponsibility/chainOfResponsibilityDriver');
ref.demoChainOfResponsibility();
res.send('/javascript/patterns/chainOfResponsibility - complete!');
});
app.get('/javascript/patterns/bridge', function(req, res){
var cmd = require('./bridge/bridgeDriver');
cmd.demoBridgePattern();
var ref = require('./bridge/bridgeDriver');
ref.demoBridgePattern();
res.send('/javascript/patterns/bridge - complete!');
});
app.get('/javascript/patterns/factory', function(req, res) {
var ref = require('./factory/factoryDriver');
ref.demoFactoryPattern();
res.send('/javascript/patterns/factory - complete!');
});
// TODO - complete
app.get('/javascript/patterns/binarySearchTree', function(req, res){
var cmd = require('./binarySearchTree/binarySearchTreeDriver');

0 comments on commit 8fc4145

Please sign in to comment.