Proxy VS Decorator design pattern

Emad ghaffari
2 min readAug 28, 2021

--

Proxy is a black box while Decorator is a white box….

Proxy lets you provide a substitute or placeholder for another object.

Decorator is also called “Smart Proxy.” This is used when you want to add functionality to an object, but not by extending that object’s type. This allows you to do so at runtime.

Proxy Pattern:

1- control the service object without clients knowing about it.
2- manage the lifecycle of the service object when clients don’t care about it.
3- introduce new proxies without changing the service or clients.[Open-Closed] Principle
4-
can hide the fact that you’re calling a remote service or control access to the object.

Proxy Example:

Decorator Pattern:

1- extend an object’s behavior without making a new subclass.
2- add or remove responsibilities from an object at runtime.
3- combine several behaviors by wrapping an object into multiple decorators.
4- You can divide a monolithic class that implements many possible variants of behavior into several smaller classes.[Single Responsibility] Principle

Decorator Example:

Proxy VS Decorator:

1- The proxy provides the same interface as the object it’s holding the reference to, and it doesn’t modify the data in any manner; it’s in contrast to Adapter and Decorator patterns which alter and decorate the functionalities of pre-existing instances respectively.

2- The Proxy usually has the information about the real subject at the compile time itself whereas the Decorator and Adapter get injected at runtime, knowing only the actual object’s interface.

3- The decorator can be used recursively, an infinite number of times, which is not possible with other models

4- A decorator adds one or more responsibilities to an object, whereas a proxy controls access to an object.

5- Proxy may restrict what a client does by controlling access to functionality; or it may restrict what a client knows by performing actions that are invisible and unknown to the client. Decorator does the opposite: it enhances what its delegate does in a way that is visible to clients.

--

--