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

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?

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

 

  • 一句话概述:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值