Last active
August 29, 2015 14:10
-
-
Save ChetHarrison/f6b018a023932097c68d to your computer and use it in GitHub Desktop.
simple example of the decorator pattern.
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
// simple example of the decorator pattern. | |
'use strict'; | |
var doccer = { | |
init: function(options) { | |
options = options || {}; | |
this.src = options.src; | |
this.decorators = options.decorators; | |
}, | |
getSrc: function() { | |
this.decorTargets.forEach(function(decor) { | |
this.src = this.decorations[decor].getSrc(this.src); | |
}, this); | |
return this.src; | |
}, | |
decorTargets: [], | |
addDecor: function(decor) { | |
this.decorTargets.push(decor); | |
}, | |
decorations: { | |
addPeriod: { | |
getSrc: function(src) { | |
return src += '.'; | |
} | |
}, | |
addChet: { | |
getSrc: function(src) { | |
return src += 'Chet'; | |
} | |
} | |
} | |
}; | |
doccer.init({ | |
src: 'Howdy! I\'m ' | |
}); | |
console.log(doccer.getSrc()); | |
doccer.addDecor('addChet'); | |
doccer.addDecor('addPeriod'); | |
console.log(doccer.getSrc()); | |
// Howdy! I'm | |
// Howdy! I'm Chet. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment