Skip to content

pattern

serhij.zolotukhin edited this page Jun 29, 2026 · 1 revision

GOF GRASP law of demeter

decorator vs proxy pattern key difference

The key difference between the Decorator and Proxy design patterns lies in their primary intent: a Decorator adds responsibilities or behaviors to an object, while a Proxy controls access to an object. While both patterns involve a wrapper object holding a reference to an inner object (composition) and often implement the same interface, their purposes are distinct:

Decorator Pattern

Intent: To dynamically add new, orthogonal behaviors or responsibilities to an object without modifying its core code (adhering to the Open/Closed Principle). Behavior: Enhances the core functionality, often by performing extra actions before or after delegating work to the original object. Clients are usually aware of the enhancements. Use Cases: Logging, buffering I/O streams, adding styling features, or applying encryption/compression to data streams. Client Relationship: The composition of decorators is typically controlled by the client.

Proxy Pattern Intent: To provide a placeholder or surrogate for another object to control access to it. Behavior: Governs access, potentially restricting operations, managing the service object's lifecycle (e.g., lazy initialization, remote access), or performing invisible background tasks like caching or security checks. Clients may assume they are interacting with the original object directly. Use Cases: Lazy loading of expensive objects (e.g., large images), access control/security, remote object communication (Remote Proxy), or logging/transaction management in frameworks. Client Relationship: The proxy often manages the creation and lifecycle of its real service object on its own, hiding this complexity from the client.

Summary of Differences Feature Decorator Pattern Proxy Pattern Primary Goal Enhance an object with new behaviors Control access to an object Interface Provides an enhanced interface, adding features Provides the exact same interface Client Awareness Enhancements are often visible to the client Often transparent to the client; acts as a placeholder Object Lifecycle Client usually controls which decorators are added Proxy often manages the creation/lifecycle of the target object Analogy Adding accessories to a car A security guard or a real estate agent

In short, a Decorator adds what an object can do, while a Proxy controls when and how an object can be accessed.

My side bar

Clone this wiki locally