设计模式(9):装饰模式 Decorator
装饰模式是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能。它是通过创建一个包装对象,也就是装饰来包裹真实的对象
装饰模式 Decorator
问题:
You want to add behavior or state to individual objects at run-time. Inheritance is not feasible because it is static and applies to an entire class..
意图:
- Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
- Client-specified embellishment of a core object by recursively wrapping it.
- Wrapping a gift, putting it in a box, and wrapping the box.