SOLID & Design Patterns Interview Question #61
What is the difference between Decorator and Proxy?
Advanced Patterns, CQRS & Domain-Driven Design Fundamentals Mid-Level Intermediate
Quick Interview Answer
Decorator adds responsibilities or behavior to an object while preserving its interface. Proxy primarily controls access to another object, for example through authorization, lazy initialization, remote communication, or access management.
Detailed Explanation
Decorator adds responsibilities or behavior to an object while preserving its interface. Proxy primarily controls access to another object, for example through authorization, lazy initialization, remote communication, or access management.
Their structures may look similar because both wrap another object. The design intent determines which pattern is being used.
Code Example
Decorator:
LoggingService -> RealService
Proxy:
AuthorizedService -> RealService
// Both may implement the same interface,
// but they solve different problems.