js设计模式——9.装饰器模式

 

装饰一个圣诞树

// 装饰器模式,让其依次执行
    var tree = {};
    tree.decorate = function() {
        console.log('Make sure the tree won\'t fall');
    };

    tree.getDecorator = function(deco){
        tree[deco].prototype = this;
        return new tree[deco];
    };

    tree.RedBalls = function() {
        this.decorate = function() {
            this.RedBalls.prototype.decorate();
            console.log('Put on some red balls');
        };
    };
    tree.BlueBalls = function() {
        this.decorate = function() {
            this.BlueBalls.prototype.decorate();
            console.log('Add blue balls');
        };
    };
    tree.Angel = function() {
        this.decorate = function() {
            this.Angel.prototype.decorate();
            console.log('An angel on the top');
        }
    };
    tree = tree.getDecorator('BlueBalls');
    tree = tree.getDecorator('Angel');
    tree = tree.getDecorator('RedBalls');
    tree.decorate();

  

posted @ 2019-06-03 18:02  惠鹏曦  阅读(282)  评论(0)    收藏  举报