`
lwhmellow
  • 浏览: 16491 次
  • 性别: Icon_minigender_1
  • 来自: 广州
最近访客 更多访客>>
社区版块
存档分类
最新评论

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

    博客分类:
  • AS3
阅读更多

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.ComponentDecorator的父类。

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



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

  • 什么时候用Decorator Pattern?

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

 

  • 一句话概述:

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

  • 大小: 62.4 KB
  • 大小: 23 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics