首页 > 代码库 > 装饰模式
装饰模式
意图
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.
当用子类扩展不切实际时。即,用子类扩展功能是可能的,但是为支持每一种功能的组合将会引子类数量的爆长。或者类的定义被隐藏或不能够被子类化。