天天看點

裝飾模式

意圖

Attach  additional  responsibilities  to  an 

object  dynamically.  Decorators  provide a flexible alternative

to subclassing for extending functionality.

動态地為對象添加額外的職責。裝飾者模式在擴充功能方面提供一個比子類化更靈活的選擇。

其類圖如下:

裝飾模式

适用範圍:

1. to add responsibilities to individual objects dynamically and

transparently, that is, without affecting other objects.

    動态地、透明地為單個對象添加職責,而不影響其他對象。

2. for responsibilities that

can be withdrawn.

    可以被撤銷的職責。

3. when  extension  by 

subclassing  is  impractical.  Sometimes  a 

large  number  of independent extensions are possible and would

produce an explosion of subclasses  to  support  every 

combination.  Or a class  definition  may  be  hidden

or otherwise unavailable for subclassing.

當用子類擴充不切實際時。即,用子類擴充功能是可能的,但是為支援每一種功能的組合将會引子類數量的爆長。或者類的定義被隐藏或不能夠被子類化。