Components of Proxy Design Pattern
1. Subject
The Subject
is an interface or an abstract class that defines the common interface shared by the RealSubject
and Proxy
classes. It declares the methods that the Proxy
uses to control access to the RealSubject
.
- Declares the common interface for both
RealSubject
andProxy
. - Usually includes the methods that the client code can invoke on the
RealSubject
and theProxy
.
2. RealSubject
The RealSubject
is the actual object that the Proxy
represents. It contains the real implementation of the business logic or the resource that the client code wants to access.
- It Implements the operations declared by the
Subject
interface. - Represents the real resource or object that the
Proxy
controls access to.
3. Proxy
The Proxy
acts as a surrogate or placeholder for the RealSubject
. It controls access to the real object and may provide additional functionality such as lazy loading, access control, or logging.
- Implements the same interface as the
RealSubject
(Subject). - Maintains a reference to the
RealSubject
. - Controls access to the
RealSubject
, adding additional logic if necessary.
Proxy Design Pattern
The Proxy Design Pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it. This pattern is useful when you want to add an extra layer of control over access to an object. The proxy acts as an intermediary, controlling access to the real object.
A real-world example can be a cheque or credit card as a proxy for what is in our bank account. It can be used in place of cash and provides a means of accessing that cash when required.
- And that’s exactly what the Proxy pattern does – ” Controls and manages access to the object they are protecting”.
- As in the decorator pattern, proxies can be chained together. The client, and each proxy, believe it is delegating messages to the real server:
Important Topics for the Proxy Design Pattern
- Chaining of Proxies
- Components of Proxy Design Pattern
- Proxy Design Pattern example
- Why do we need Proxy Design Pattern?
- When to use Proxy Design Pattern?
- When not to use Proxy Design Pattern?