Last active
August 29, 2015 14:22
-
-
Save elpete/821be897857f6640c7f3 to your computer and use it in GitHub Desktop.
Singleton Constructor Factory
This file contains 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 Box(x,y) { | |
this.x = x; | |
this.y = y; | |
} | |
var Box1 = Singleton(Box, { argumentCheck: 'equal'}); | |
var Box2 = Singleton(Box, { argumentCheck: 'equal'}); | |
var obj1 = new Box1(1,2); | |
var obj2 = new Box2(1,2); | |
var obj3 = new Box1(1,2); | |
console.log('obj1 === obj2', obj1 === obj2); | |
console.log('obj1 === obj3', obj1 === obj3); | |
console.log('obj2 === obj3', obj2 === obj3); | |
var SomeService = Singleton(function() { | |
return { | |
funcOne: function() { | |
console.log('funcOne'); | |
}, | |
funcTwo: function(text) { | |
console.log(text || 'No text provided.'); | |
} | |
}; | |
}, { argumentCheck: 'less' }); | |
var service1 = new SomeService(); | |
var service2 = new SomeService(); | |
console.log('service1 === service2', service1 === service2); | |
var service3 = new SomeService({ someOption: true }); |
This file contains 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 Singleton(obj, options) { | |
var defaultOptions = { | |
ignoreArgumentUniqueness: false, | |
defaultHash: 'asdfii12300fkadfn213if0', | |
argumentCheck: 'none' // 'none', 'less', 'equal', | |
}; | |
function mergeObjects(obj1, obj2) { | |
var obj3 = {}; | |
for (var attrname1 in obj1) { obj3[attrname1] = obj1[attrname1]; } | |
for (var attrname2 in obj2) { obj3[attrname2] = obj2[attrname2]; } | |
return obj3; | |
} | |
options = mergeObjects(defaultOptions, options); | |
var __instances = {}; | |
return function() { | |
var args = Array.prototype.slice.call(arguments); | |
if (!options.ignoreArgumentUniqueness && options.argumentCheck === 'less') { | |
if (args.length > obj.length) { | |
throw new Error('Too many arguments provided. Expected: ' + obj.length + '. Actual: ' + args.length + '.'); | |
} | |
} | |
if (!options.ignoreArgumentUniqueness && options.argumentCheck === 'equal') { | |
if (args.length !== obj.length) { | |
var manyOrFew = args.length > obj.length ? 'many' : 'few'; | |
throw new Error('Too ' + manyOrFew + ' arguments provided. Expected: ' + obj.length + '. Actual: ' + args.length + '.'); | |
} | |
} | |
var argHash = options.ignoreArgumentUniqueness ? options.defaultHash : JSON.stringify(args); | |
if (!__instances.hasOwnProperty(argHash)) { | |
var createObj = function() { | |
return obj.apply(this, args); | |
}; | |
createObj.prototype = obj.prototype; | |
__instances[argHash] = new createObj(); | |
} | |
return __instances[argHash]; | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment