Could anybody help me to implement the Decorator design pattern in javascript. I have a TankBase entity:
TankBase = function (x, y, width, height, direction, imageOptions) {
TankBase.base.call(this, x, y, width, height, imageOptions);
this.speed = 250;
this.direction = direction;
this.render = function (drawEngine) {
drawEngine.render();
};
...
}
I want to add a new functionality using the Decorator pattern. For example, I want to modify the render() function and draw a health indicator under a tank:
var TankHealthDecorator = function (tank) {
var _tank = tank;
this.render = function (drawEngine) {
// draw a health indicator
...
_tank.render(drawEngine);
};
}
Usage:
var tank = new TankHealthDecorator(new HeavyTank());
where HeavyTank inherits TankBase.
How should I modify TankHealthDecorator() to use it like a wrapper for a tank instance?
EDIT:
Thank you, Paul, for a great article:
I would start here: addyosmani.com/blog/decorator-pattern Good write up. – Paul