繁体   English   中英

如何在javascript中实现Decorator Design模式?

[英]How to implement the Decorator Design pattern in javascript?

谁能帮助我在javascript中实现Decorator设计模式。 我有一个TankBase实体:

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();
    };
    ...
}

我想使用装饰器模式添加新功能。 例如,我想修改render()函数并在罐下绘制一个健康指示器:

var TankHealthDecorator = function (tank) {
    var _tank = tank;
    this.render = function (drawEngine) {
        // draw a health indicator
        ...
        _tank.render(drawEngine);
    };
}

用法:

var tank = new TankHealthDecorator(new HeavyTank());

HeavyTank在其中继承TankBase。

我应该如何修改TankHealthDecorator()使其像坦克实例的包装一样使用?

编辑:

保罗,谢谢你的精彩文章:

我将从这里开始:addyosmani.com/blog/decorator-pattern写得很好。 –保罗

从功能上讲,我认为您所拥有的非常接近。 我将存储原始的渲染功能,分配一个新的渲染功能,然后仅在装饰后的功能中应用它。 我也没有看到需要将装饰器创建为对象,但这可能更像是一种偏好。

var DrawEngine = { render: function() {
    console.log('render');
} };

var TankBase = function (x, y, width, height, direction, imageOptions) {
    this.speed = 250;
    this.direction = direction;
    this.render = function (drawEngine) {
        drawEngine.render();
    };
};

var HeavyTank = function() {
    TankBase.apply(this, arguments);
    this.render = function() {
        console.log('heavyTank Render');
    }
}

function DecorateTankWithHealthIndicator (tank) {
    var oRender = tank.render;
    tank.render = function (drawEngine) {
        console.log('draw a health indicator');
        oRender.apply(tank, arguments);
    };
};

var btank = new TankBase();
var htank = new HeavyTank();
btank.render(DrawEngine);
htank.render(DrawEngine);
DecorateTankWithHealthIndicator(btank);
DecorateTankWithHealthIndicator(htank);
btank.render(DrawEngine);
htank.render(DrawEngine);

在以下方法中,将tankBase对象传递给tankHealth函数。 在tankHealth函数中修改tankBase对象,将tankBase对象保存在var that 改造后that被返回修改后的tankBase对象。

var TankBase = function () {
        this.render = function () {
            console.log('tankBase')
        };
    }

var TankHealthDecorator = function (tank) {
    var that = tank;

    that.render = function() {
        console.log('tankHealth')
    };

    return that;
}

window.onload = function(){
    var tankBase = new TankBase();
    var testHealth = new TankHealthDecorator(new TankBase());

    tankBase.render();     // tank base
    testHealth.render();   // tank health
};

装饰图案

这是Decorator模式的粗略实现-AKA: 包装器

背景:

修饰模式通常用于添加额外的责任到对象动态地将请求转发到其接受组件, 和|或解耦深继承层次结构集成类型 (不与组合模式混淆-这是集成层次 -使用Component-Leaf结构,允许在统一的,分形的拓扑结构中进行同义请求(平等对待父级和子级))。

function AbstractComponent(){
    this.draw = function(){
        console.log('@AbstractComponent | #draw');
    };
}

function AbstractDecorator(){
    AbstractComponent.call(this);  // Inherit AbstractComponent Interface
    this.notify = function(){
        console.log('@AbstractDecorator | #notify');
    };
}

function ConcreteComponent(options){
    AbstractComponent.call(this);  // Inherit AbstractComponent Interface
    this.fill = function(){
        console.log('@ConcreteComponent | #fill');
    };
}

function ConcreteDecorator(Component){
    AbstractDecorator.call(this);  // Inherit AbstractDecorator Interface
    function PrivateResponsibility(){
        console.log('@ConcreteDecorator | #PrivateResponsibility');
    }
    this.additionalResponsibility = function(){
        console.log('@ConcreteDecorator | #additionalResponsibility');
    };
    this.draw = function(){
        console.log('@ConcreteDecorator | #draw-Component.draw');
        // ... additional logic
        PrivateResponsibility();
        Component.draw();
    };
    this.fill = function(){
        console.log('@ConcreteDecorator | #fill-Component.fill');
        Component.fill();
    };
}

var concreteComponent = new ConcreteComponent();
concreteComponent = new ConcreteDecorator(concreteComponent);  // use same variable name as to allow client code to remain the same

//CLIENT CODE
concreteComponent.draw();
concreteComponent.fill();
concreteComponent.notify();
concreteComponent.additionalResponsibility();

以下是我对功能(对象)装饰的理解

 function api_decorator(some_data){
     return function decorator(func){
        return function new_function(args){
         /* do somethinfg with data before and after func */
         console.log("I'm code before with data: "+some_data);
         func(args);
         console.log("I'm code after");
    }
  }
}

function somefunc(data){
    console.log("Hi, I'm func "+data);
}

somefunc("without decoration")
/* injecting somefunc in decorator api */
somefunc=api_decorator("data needed for api")(somefunc)
/* calling decorated with api somefunc */
somefunc("with decoration")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM