The Proxy Design Pattern
The Proxy pattern provides a surrogate or placeholder for another object to control access to it.
Use the Proxy pattern when you need to control access to an object, either to protect the real object or to control its instantiation and removal.
- TypeStructural
- Time ComplexityO(1) Constant Time – The algorithm runs in constant time, irrespective of the input size. Operations like accessing an array element fall into this category.
- Efficiency7
-
Learning Effort
- Average6
- Conceptual6
- Debug and Maintain5
- Implementation4
- Prerequisites5
- Versatility8
Things you should know before you start
- Delegation Understanding of delegation, as the proxy delegates requests to the real object.
- Encapsulation Understanding of encapsulation to protect the real object.
Minimal Example
Implementing lazy loading for a heavy image object that consumes a lot of memory.
class ImageProxy implements Image { private realImage: RealImage; public display() { if (!this.realImage) { this.realImage = new RealImage(); } this.realImage.display(); } } const image = new ImageProxy(); image.display();
What’s happening in thie example
In this example, the `ImageProxy` class controls the instantiation of the real `Image` object. The `clientCode` shows how the proxy ensures that the real object is only instantiated when the `display` method is called.
How the Proxy Works
The Proxy pattern is used when you need to represent an object that is complex or time-consuming to create. It acts as a stand-in and controls access to the real object.
This pattern is particularly useful in scenarios like lazy loading, controlling access, or logging, where you need an intermediary between the client and the real object.
Entities in the Proxy Design Pattern
-
ProxyThe Proxy class represents the real object and controls access to it.
- Proxy has a one-to-one delegates relationship with RealObject
What to watch out for when using or considering this design pattern.
Avoid using the Proxy pattern when it’s not necessary to have an intermediary between the client and the real object, as it can introduce extra layers of complexity.
Design patterns often confused with the Proxy Design Pattern
The following design patterns are often confused with the Proxy
for various resons.
If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.
-
Adapter – The Adapter pattern allows objects with incompatible interfaces to collaborate.
The Adapter pattern is similar to the Proxy in the following ways.
- Both are structural design patterns.
- Both act as intermediaries between two different interfaces or objects.
- Proxy controls access to an object, while Adapter makes two incompatible interfaces compatible.
- Proxy usually has the same interface as its subject, whereas Adapter may have a different interface.
-
Decorator – The Decorator pattern attaches additional responsibilities to an object dynamically.
The Decorator pattern is similar to the Proxy in the following ways.
- Both are structural design patterns.
- Both involve wrapping an object.
- Proxy controls access to the object, while Decorator adds new functionalities.
- Proxy usually doesn’t alter the behavior of the object, whereas Decorator does.