深入理解javascript系列(29):设计模式之装饰者模式详解

上传人:bin****86 文档编号:59392371 上传时间:2018-11-07 格式:DOCX 页数:6 大小:16.92KB
返回 下载 相关 举报
深入理解javascript系列(29):设计模式之装饰者模式详解_第1页
第1页 / 共6页
深入理解javascript系列(29):设计模式之装饰者模式详解_第2页
第2页 / 共6页
深入理解javascript系列(29):设计模式之装饰者模式详解_第3页
第3页 / 共6页
深入理解javascript系列(29):设计模式之装饰者模式详解_第4页
第4页 / 共6页
深入理解javascript系列(29):设计模式之装饰者模式详解_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《深入理解javascript系列(29):设计模式之装饰者模式详解》由会员分享,可在线阅读,更多相关《深入理解javascript系列(29):设计模式之装饰者模式详解(6页珍藏版)》请在金锄头文库上搜索。

1、我真正系统地接触和学习党的基本知识是在这次中级党校的培训班上。通过学习,了解了党的发展历程,对党的性质、宗旨、任务等基本知识有了进一步的了解深入理解JavaScript系列(29):设计模式之装饰者模式详解这篇文章主要介绍了深入理解JavaScript系列(29):设计模式之装饰者模式详解,装饰者用用于包装同接口的对象,不仅允许你向方法添加行为,而且还可以将方法设置成原始对象调用(例如装饰者的构造函数),需要的朋友可以参考下介绍装饰者提供比继承更有弹性的替代方案。 装饰者用用于包装同接口的对象,不仅允许你向方法添加行为,而且还可以将方法设置成原始对象调用(例如装饰者的构造函数)。装饰者用于通过

2、重载方法的形式添加新功能,该模式可以在被装饰者前面或者后面加上自己的行为以达到特定的目的。正文那么装饰者模式有什么好处呢?前面说了,装饰者是一种实现继承的替代方案。当脚本运行时,在子类中增加行为会影响原有类所有的实例,而装饰者却不然。取而代之的是它能给不同对象各自添加新行为。如下代码所示:代码如下:/需要装饰的类(函数)function Macbook() this.cost = function () return 1000;function Memory(macbook) this.cost = function () return macbook.cost() + 75;function

3、 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();下面是另一个实例,当我们在装饰者对象上调用performTask时,它不仅具有一些装

4、饰者的行为,同时也调用了下层对象的performTask函数。代码如下: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 =

5、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

6、);decorator2.performTask();再来一个彻底的例子:代码如下:var tree = ;tree.decorate = function () console.log(Make sure the tree wont fall);tree.getDecorator = function (deco) treedeco.prototype = this;return new treedeco;tree.RedBalls = function () this.decorate = function () this.RedBalls.prototype.decorate(); /

7、第7步:先执行原型(这时候是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步:先执行原型的decorate方法,也就是tree.decorate()console.log(Add blue balls); / 第2步 再输出blue/ 将这2步作为BlueBa

8、lls的decorate方法;tree.Angel = function () this.decorate = function () this.Angel.prototype.decorate(); / 第4步:先执行原型(这时候是BlueBalls了)的decorate方法console.log(An angel on the top); / 第5步 再输出angel/ 将这2步作为Angel的decorate方法;tree = tree.getDecorator(BlueBalls); / 第3步:将BlueBalls对象赋给tree,这时候父原型里的getDecorator依然可用tr

9、ee = tree.getDecorator(Angel); / 第6步:将Angel对象赋给tree,这时候父原型的父原型里的getDecorator依然可用tree = tree.getDecorator(RedBalls); / 第9步:将RedBalls对象赋给treetree.decorate(); / 第10步:执行RedBalls对象的decorate方法总结装饰者模式是为已有功能动态地添加更多功能的一种方式,把每个要装饰的功能放在单独的函数里,然后用该函数包装所要装饰的已有函数对象,因此,当需要执行特殊行为的时候,调用代码就可以根据需要有选择地、按顺序地使用装饰功能来包装对象。优点是把类(函数)的核心职责和装饰功能区分开了。对党的认识也有了进一步的提高。才真正体会到了中国共产党的伟大、光荣和正确,更感到只有中国共产党是全中国最广大人民利益的忠实代表

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 总结/报告

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号