博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
装饰者模式
阅读量:7026 次
发布时间:2019-06-28

本文共 2876 字,大约阅读时间需要 9 分钟。

  装饰者模式提供比继承更有弹性的替代方案。

  装饰者用于包装同接口的对象,不仅允许你向方法添加行为,而且还可以将方法设置成原始对象调用(例如装饰者的构造函数)。

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

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

//需要装饰的类(函数)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());

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

  再来一个彻底的例子:

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步:先执行原型(这时候是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步作为BlueBalls的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依然可用tree = tree.getDecorator('Angel'); // 第6步:将Angel对象赋给tree,这时候父原型的父原型里的getDecorator依然可用tree = tree.getDecorator('RedBalls'); // 第9步:将RedBalls对象赋给treetree.decorate(); // 第10步:执行RedBalls对象的decorate方法

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

 

转载地址:http://jooxl.baihongyu.com/

你可能感兴趣的文章
Mac电脑C语言开发的入门帖
查看>>
洛谷P4242 树上的毒瘤
查看>>
JQ实现树形菜单点击高亮
查看>>
函数动态参数
查看>>
华为机试题 -- 明明的随机数
查看>>
一道简单的数学题
查看>>
为什么 执行typeof null时会返回字符串“object”?
查看>>
JavaScript关于闭包的理解和实例
查看>>
jquery-ui-widget
查看>>
VC Error spawning cl.exe
查看>>
IIS连接数据库:数据库连接出错,请检查连接字串
查看>>
centos7救援模式--rescue模式
查看>>
C++ 输出到文本文件
查看>>
sql Lloader
查看>>
使用python学习【机器学习】需要安装的库~
查看>>
第一次作业+105032014098
查看>>
Codeforces 832B: Petya and Exam
查看>>
axios链接带参数_VUE升级(全面解析vuecil3/vuecil4的vue.config.js等常用配置,配置axios)...
查看>>
vue warning如何去掉_详解vue组件三大核心概念
查看>>
qt mysql md5加密_Qt 给密码进行MD5加密
查看>>