Skip to content

Instantly share code, notes, and snippets.

@mateusvahl
Last active May 3, 2016 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mateusvahl/5de19d24488b440bb6beb4ce8435cc03 to your computer and use it in GitHub Desktop.
Save mateusvahl/5de19d24488b440bb6beb4ce8435cc03 to your computer and use it in GitHub Desktop.
// Logged User can access they feed
var LoggedUser = {
neewsFeed: function() {
console.log('/feed');
// return redirect('/feed')
}
}
// Let's represent the absence of an credential user
// https://en.wikipedia.org/wiki/Null_Object_pattern
var guestUser = {
neewsFeed: function() {
console.log('/login');
// return redirect('/login');
}
}
// Dispatch function based on user credentials
function userFactory(login, pass) {
if(login === 'user' && pass === 123) {
return LoggedUser;
}
return guestUser;
}
// This allow us to create generic methods!
// I don't want know if I have an logged user or not,
// just give-me an object to send messages
userFactory('user', 123).neewsFeed();
userFactory('', '').neewsFeed();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment