(29)設(shè)計(jì)模式之裝飾者模式

2018-02-24 15:25 更新

介紹

裝飾者提供比繼承更有彈性的替代方案。 裝飾者用用于包裝同接口的對(duì)象,不僅允許你向方法添加行為,而且還可以將方法設(shè)置成原始對(duì)象調(diào)用(例如裝飾者的構(gòu)造函數(shù))。

裝飾者用于通過(guò)重載方法的形式添加新功能,該模式可以在被裝飾者前面或者后面加上自己的行為以達(dá)到特定的目的。

正文

那么裝飾者模式有什么好處呢?前面說(shuō)了,裝飾者是一種實(shí)現(xiàn)繼承的替代方案。當(dāng)腳本運(yùn)行時(shí),在子類(lèi)中增加行為會(huì)影響原有類(lèi)所有的實(shí)例,而裝飾者卻不然。取而代之的是它能給不同對(duì)象各自添加新行為。如下代碼所示:

//需要裝飾的類(lèi)(函數(shù))
function Macbook() {
    this.cost = function () {
        return 1000;
    };
}

function Memory(macbook) {
    this.cost = function () {
        return macbook.cost() + 75;
    };
}

function BlurayDrive(macbook) {
    this.cost = function () {
        return macbook.cost() + 300;
    };
}

function Insurance(macbook) {
    this.cost = function () {
        return macbook.cost() + 250;
    };
}

// 用法
var myMacbook = new Insurance(new BlurayDrive(new Memory(new Macbook())));
console.log(myMacbook.cost());

下面是另一個(gè)實(shí)例,當(dāng)我們?cè)谘b飾者對(duì)象上調(diào)用performTask時(shí),它不僅具有一些裝飾者的行為,同時(shí)也調(diào)用了下層對(duì)象的performTask函數(shù)。

function ConcreteClass() {
    this.performTask = function () {
        this.preTask();
        console.log('doing something');
        this.postTask();
    };
}

function AbstractDecorator(decorated) {
    this.performTask = function () {
        decorated.performTask();
    };
}

function ConcreteDecoratorClass(decorated) {
    this.base = AbstractDecorator;
    this.base(decorated);

    decorated.preTask = function () {
        console.log('pre-calling..');
    };

    decorated.postTask = function () {
        console.log('post-calling..');
    };

}

var concrete = new ConcreteClass();
var decorator1 = new ConcreteDecoratorClass(concrete);
var decorator2 = new ConcreteDecoratorClass(decorator1);
decorator2.performTask();

再來(lái)一個(gè)徹底的例子:

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(); // 第7步:先執(zhí)行原型(這時(shí)候是Angel了)的decorate方法
        console.log('Put on some red balls'); // 第8步 再輸出 red
        // 將這2步作為RedBalls的decorate方法
    }
};

tree.BlueBalls = function () {
    this.decorate = function () {
        this.BlueBalls.prototype.decorate(); // 第1步:先執(zhí)行原型的decorate方法,也就是tree.decorate()
        console.log('Add blue balls'); // 第2步 再輸出blue
        // 將這2步作為BlueBalls的decorate方法
    }
};

tree.Angel = function () {
    this.decorate = function () {
        this.Angel.prototype.decorate(); // 第4步:先執(zhí)行原型(這時(shí)候是BlueBalls了)的decorate方法
        console.log('An angel on the top'); // 第5步 再輸出angel
        // 將這2步作為Angel的decorate方法
    }
};

tree = tree.getDecorator('BlueBalls'); // 第3步:將BlueBalls對(duì)象賦給tree,這時(shí)候父原型里的getDecorator依然可用
tree = tree.getDecorator('Angel'); // 第6步:將Angel對(duì)象賦給tree,這時(shí)候父原型的父原型里的getDecorator依然可用
tree = tree.getDecorator('RedBalls'); // 第9步:將RedBalls對(duì)象賦給tree

tree.decorate(); // 第10步:執(zhí)行RedBalls對(duì)象的decorate方法

總結(jié)

裝飾者模式是為已有功能動(dòng)態(tài)地添加更多功能的一種方式,把每個(gè)要裝飾的功能放在單獨(dú)的函數(shù)里,然后用該函數(shù)包裝所要裝飾的已有函數(shù)對(duì)象,因此,當(dāng)需要執(zhí)行特殊行為的時(shí)候,調(diào)用代碼就可以根據(jù)需要有選擇地、按順序地使用裝飾功能來(lái)包裝對(duì)象。優(yōu)點(diǎn)是把類(lèi)(函數(shù))的核心職責(zé)和裝飾功能區(qū)分開(kāi)了。

以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)