intermediateDecorator Pattern

Decorator and Proxy have nearly identical class structures. How do you tell which pattern you're actually looking at?

Both patterns wrap an object behind the same interface it implements, so structurally a decorator and a proxy can look almost indistinguishable in a class diagram. The real difference is intent. A decorator's job is adding new behavior that composes freely with other decorators -- it's common to stack several of them together, like several toppings on a pizza or several stream wrappers around a file. A proxy's job is controlling access to the real object -- lazy loading, caching, permission checks, or logging -- and proxies are typically used singly rather than stacked. A useful question to ask when reading unfamiliar code: is this wrapper adding a genuinely new capability, or deciding whether, when, and how the real object gets used?

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

Next Step

Continue to What thread-safety bug can a naive lazy-loading proxy have, and how would you fix it?← Back to all Low-Level Design & Design Patterns questions