Skip to content

Instantly share code, notes, and snippets.

@ChetHarrison
Last active August 29, 2015 14:10
Show Gist options
  • Save ChetHarrison/f6b018a023932097c68d to your computer and use it in GitHub Desktop.
Save ChetHarrison/f6b018a023932097c68d to your computer and use it in GitHub Desktop.
simple example of the decorator pattern.
// 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