Last active
August 29, 2015 14:16
-
-
Save alistair/6b8bbb9ea7f36c1e42bf to your computer and use it in GitHub Desktop.
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
====================================================== | |
====================================================== | |
//File toggle.js | |
// All the logic for getting toggle information | |
exports = function FeatureToggle(toggleName, offFunc, onFunc) { | |
return function() { | |
if (toggleOn(toggleName)) | |
{ | |
onFunc.apply(this, arguments); // | |
} else | |
{ | |
offFunc.apply(this, arguments); | |
} | |
} | |
} | |
====================================================== | |
====================================================== | |
// file profile-store.js | |
var toggle = require('FeatureToggle'); | |
var profileStoreOn = { | |
method1 : function() { /* does some stuff */ } | |
method2 : function() {} | |
}; | |
var profileStoreOff = function() { | |
method1 : function() { /* does some nothing, it is a null object */ } | |
method2 : function() {} | |
} | |
exports = toggle('feature', profileStoreOn, profileStoreOff); | |
====================================================== | |
====================================================== | |
// file some-other-store.js | |
var toggle = require('FeatureToggle'); | |
var on = function() { /* does some stuff */ }; | |
var off = function() { /* does some other stuff */ }; | |
exports = { | |
method1 : toggle('tooglename', on, off) | |
}; | |
====================================================== | |
====================================================== | |
// file some-file-using-those-others | |
var profileStore = require('profile-store'); | |
var otherStore = require('some-other-store'); | |
//What is important to understand here is that guess what.... | |
//the consuming code doesn't give a ... about whether there is a toggle or not. | |
profileStore.method1(); | |
otherStore.method1(); | |
// we can do it | |
// we can write code without if statements. | |
// we can fix it! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment