Decorator
Keeps the interface and adds responsibility, and it stacks. Its diagram is identical to Proxy’s – the difference is that a decorator adds behavior while a proxy controls access to the same behavior. Kotlin’s interface delegation makes a decorator a one-liner, which is why this is the wrapper you will actually reach for.
Structure
classDiagram
class Component {
<<interface>>
+operation()
}
class ConcreteComponent {
+operation()
}
class Decorator {
#inner: Component
+operation()
}
class LoggingDecorator {
+operation()
}
class CachingDecorator {
+operation()
}
Component <|.. ConcreteComponent
Component <|.. Decorator
Decorator o--> Component : wraps one
Decorator <|-- LoggingDecorator
Decorator <|-- CachingDecorator
Telling It Apart
Patterns whose structure looks the same but whose intent does not. See the Confusing Pairs reference.
- Proxy — Keep the interface and control access – lazy loading, caching, permissions, remoting.
- Adapter — Change an interface so two otherwise incompatible things can work together.
- Composite — Treat individual objects and compositions of objects uniformly.
- Chain of Responsibility — Pass a request along a chain until some handler takes it, and can stop it.