Decorator 模式通过包装对象来动态地为其添加行为或责任 — 不修改原始类。它提供了一个灵活的替代方案来代替继承以扩展功能,让你能够组合行为。
Decorator 模式的作用
DECORATOR → WRAP an object to add behavior, keeping the same interface:
→ the decorator wraps the original, adds its behavior, and delegates to the original
→ you can STACK decorators (wrap a wrapped object) → compose multiple behaviors
→ adds functionality WITHOUT modifying the original class or using subclassing
→ "wrap to extend" — flexible, composable behavior addition
示例
coffee = { : , : };
() {
{ : c.() + , : c.() + };
}
() {
{ : c.() + , : c.() + };
}
coffee = ((coffee));
coffee.();
