Skip to content

Instantly share code, notes, and snippets.

@kampfer
Created March 6, 2013 02:46
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 kampfer/5096309 to your computer and use it in GitHub Desktop.
Save kampfer/5096309 to your computer and use it in GitHub Desktop.
//Class to be decorated
function Coffee(){
this.cost = function(){
return 1;
};
}
//Decorator A
function Milk(coffee){
this.cost = function(){
return coffee.cost() + 0.5;
};
}
//Decorator B
function Whip(coffee){
this.cost = function(){
return coffee.cost() + 0.7;
};
}
//Decorator C
function Sprinkles(coffee){
this.cost = function(){
return coffee.cost() + 0.2;
};
}
//Here's one way of using it
var coffee = new Milk(new Whip(new Sprinkles(new Coffee())));
alert( coffee.cost() );
//Here's another
var coffee = new Coffee();
coffee = new Sprinkles(coffee);
coffee = new Whip(coffee);
coffee = new Milk(coffee);
alert(coffee.cost());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment