Problem Statement
Explain the Decorator and Proxy patterns, how they differ and when you'd use each in system design.
Explanation
The Decorator pattern dynamically attaches responsibilities to an object at runtime without changing its class definition. For example you might wrap a `TextView` with `ScrollDecorator`, `BorderDecorator`, then pass it to a client. Each decorator adds new behaviour. /n/n The Proxy pattern provides a placeholder or surrogate for another object to control access, reduce cost, or add logging/security. For example, a `RemoteProxy` might represent a remote service locally and manage network communication. You'd use Decorator when you need to add flexible behaviour, and Proxy when you need to control access or hide complexity of an object. Knowing these distinctions in interviews shows your depth of design thinking.
Practice Sets
This question appears in the following practice sets: