加载中...

(29)设计模式之装饰者模式


介绍

装饰者提供比继承更有弹性的替代方案。 装饰者用用于包装同接口的对象,不仅允许你向方法添加行为,而且还可以将方法设置成原始对象调用(例如装饰者的构造函数)。

装饰者用于通过重载方法的形式添加新功能,该模式可以在被装饰者前面或者后面加上自己的行为以达到特定的目的。

正文

那么装饰者模式有什么好处呢?前面说了,装饰者是一种实现继承的替代方案。当脚本运行时,在子类中增加行为会影响原有类所有的实例,而装饰者却不然。取而代之的是它能给不同对象各自添加新行为。如下代码所示:

  1. //需要装饰的类(函数)
  2. function Macbook() {
  3. this.cost = function () {
  4. return 1000;
  5. };
  6. }
  7. function Memory(macbook) {
  8. this.cost = function () {
  9. return macbook.cost() + 75;
  10. };
  11. }
  12. function BlurayDrive(macbook) {
  13. this.cost = function () {
  14. return macbook.cost() + 300;
  15. };
  16. }
  17. function Insurance(macbook) {
  18. this.cost = function () {
  19. return macbook.cost() + 250;
  20. };
  21. }
  22. // 用法
  23. var myMacbook = new Insurance(new BlurayDrive(new Memory(new Macbook())));
  24. console.log(myMacbook.cost());

下面是另一个实例,当我们在装饰者对象上调用performTask时,它不仅具有一些装饰者的行为,同时也调用了下层对象的performTask函数。

  1. function ConcreteClass() {
  2. this.performTask = function () {
  3. this.preTask();
  4. console.log('doing something');
  5. this.postTask();
  6. };
  7. }
  8. function AbstractDecorator(decorated) {
  9. this.performTask = function () {
  10. decorated.performTask();
  11. };
  12. }
  13. function ConcreteDecoratorClass(decorated) {
  14. this.base = AbstractDecorator;
  15. this.base(decorated);
  16. decorated.preTask = function () {
  17. console.log('pre-calling..');
  18. };
  19. decorated.postTask = function () {
  20. console.log('post-calling..');
  21. };
  22. }
  23. var concrete = new ConcreteClass();
  24. var decorator1 = new ConcreteDecoratorClass(concrete);
  25. var decorator2 = new ConcreteDecoratorClass(decorator1);
  26. decorator2.performTask();

再来一个彻底的例子:

  1. var tree = {};
  2. tree.decorate = function () {
  3. console.log('Make sure the tree won\'t fall');
  4. };
  5. tree.getDecorator = function (deco) {
  6. tree[deco].prototype = this;
  7. return new tree[deco];
  8. };
  9. tree.RedBalls = function () {
  10. this.decorate = function () {
  11. this.RedBalls.prototype.decorate(); // 第7步:先执行原型(这时候是Angel了)的decorate方法
  12. console.log('Put on some red balls'); // 第8步 再输出 red
  13. // 将这2步作为RedBalls的decorate方法
  14. }
  15. };
  16. tree.BlueBalls = function () {
  17. this.decorate = function () {
  18. this.BlueBalls.prototype.decorate(); // 第1步:先执行原型的decorate方法,也就是tree.decorate()
  19. console.log('Add blue balls'); // 第2步 再输出blue
  20. // 将这2步作为BlueBalls的decorate方法
  21. }
  22. };
  23. tree.Angel = function () {
  24. this.decorate = function () {
  25. this.Angel.prototype.decorate(); // 第4步:先执行原型(这时候是BlueBalls了)的decorate方法
  26. console.log('An angel on the top'); // 第5步 再输出angel
  27. // 将这2步作为Angel的decorate方法
  28. }
  29. };
  30. tree = tree.getDecorator('BlueBalls'); // 第3步:将BlueBalls对象赋给tree,这时候父原型里的getDecorator依然可用
  31. tree = tree.getDecorator('Angel'); // 第6步:将Angel对象赋给tree,这时候父原型的父原型里的getDecorator依然可用
  32. tree = tree.getDecorator('RedBalls'); // 第9步:将RedBalls对象赋给tree
  33. tree.decorate(); // 第10步:执行RedBalls对象的decorate方法

总结

装饰者模式是为已有功能动态地添加更多功能的一种方式,把每个要装饰的功能放在单独的函数里,然后用该函数包装所要装饰的已有函数对象,因此,当需要执行特殊行为的时候,调用代码就可以根据需要有选择地、按顺序地使用装饰功能来包装对象。优点是把类(函数)的核心职责和装饰功能区分开了。


还没有评论.