[AS3设计模式][原理][装饰模式-Decorator Patte

系统 1559 0

Well,这几天在看O'Reilly的ActionScript 3.0 Design Patterns.英文版的说 。主要还是,I'm sick of the translation edition.

不过看英文版的书,感觉很难进入脑子......So,take notes......

 

今天主题是“Decorator Pattern,装饰模式”的原理。

  • 什么是装饰模式?

  原文 :The Decorator pattern addresses the issue of maintaining the structure while having
the ability to make changes by decorating the different components that make up the
application. The decorations are composed of descriptions and/or methods used to
wrap different objects in the application. As you will see, this design pattern allows
you to mix and match all the different components and decorations to optimize flexibility
and expandability, while core structure classes remain unaltered.

翻译 :装饰模式强调,在保留程序原有结构的同时,通过装饰程序中的不同组件,来实现程序功能的更改。这些"装饰用品"由用于包装不同对象的说明和方法组成。正如你所看到的,装饰模式允许你通过混合使用不同的"组件"和"装饰",来优化程序的灵活性和延展性,同时程序的核心结构保持不变。

  • 装饰模式的特点

(1)    装饰对象和真实对象有相同的接口。这样客户端对象就可以以和真实对象相同的方式和装饰对象交互。
(2)    装饰对象包含一个真实对象的索引(reference)
(3)    装饰对象接受所有的来自客户端的请求。它把这些请求转发给真实的对象。

  •  装饰模型-The Decorator Model

看图:

1.其中 Component、 Decorator 两个类是抽象类。名字带有Concrete的都是派生类。

2. Component Decorator 的父类。

这样就是说,无论是 Concrete Component 抑或是 Decorator Concrete Decoration ,这些类的父类(父父类)都是 Component



上面的图显示,一个 Concrete Component 被一个 Concrete Decorator 装饰后变成#1,之后又被装饰成#2、#3。而最终,#3仍然是一个 Component 类的实例。这样被装饰过的对象依然能被同一个变量所引用。

  • 什么时候用Decorator Pattern?

当你希望你的子类有各自的特性,并且不想通过修改父类实现的时候,就可以用装饰模式。

 

  • 一句话概述:

装饰模式使你能在不修改父类的情况下(即保持原程序设计不变),给子类添加不同的功能。

[AS3设计模式][原理][装饰模式-Decorator Pattern]


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论