Паттерны / Предыдущие версии / Структурные паттерны
image

//Component
function Shape() {
    //Operation()
    this.getInfo = function () {
        return "shape";
    }

    this.showInfo = function () {
        console.log(this.getInfo());
    }
}

//ConcreteComponent
function Square() {
    Shape.apply(this);

    //Operation()
    this.getInfo = function() {
        return "square";
    }
}

//Decorator
function ShapeDecorator(shape)  {
    this.shape = shape;
    Shape.apply(this);

    //Operation()
    this.getInfo = function () {
        return this.shape.getInfo();
    }
}

//ConcreteDecorator
function ColorShape(shape, color) {
    ShapeDecorator.apply(this, arguments);

    this.getInfo = function () {
        return color + " " + this.shape.getInfo();
    }
}

//ConcreteDecorator
function ShadowShape(shape) {
    ShapeDecorator.apply(this, arguments);

    this.getInfo = function () {
        return this.shape.getInfo() + " with shadow";
    }
}

//Client
var square = new Square();
square.showInfo();
//printed: square

var colorShape = new ColorShape(square, "red");
colorShape.showInfo();
//printed: red square

var shadowShape = new ShadowShape(colorShape);
shadowShape.showInfo();
//printed: red square with shadow