装饰图案与分类 [英] Decorator pattern versus sub classing
问题描述
我可以通过添加子类来解决添加功能的问题,然后为什么要使用装饰器模式装饰器模式的真正优势?
装饰器模式可以用于
使得可以在运行时扩展(装饰)
某个对象
的功能。
装饰器模式的整个点是动态添加额外的行为/功能,这当然不可能在设计时间。
从同一篇文章:
装饰模式是一个
替代子类化。
子类在编译
时间时添加行为,更改会影响原始类的所有
实例;
装饰可以为单个对象的
运行时提供新的行为。
I can solve the problem of adding functionality by adding sub classing then why should I use decorator pattern what's the real advantage of decorator pattern ?
from Decorator pattern at wikipedia
The decorator pattern can be used to make it possible to extend (decorate) the functionality of a certain object at runtime.
The whole point of decorator pattern is to dynamically add additional behaviour/functionality, which is of course not possible at design time.
from the same article:
The decorator pattern is an alternative to subclassing. Subclassing adds behavior at compile time, and the change affects all instances of the original class; decorating can provide new behavior at runtime for individual objects.
这篇关于装饰图案与分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!