Proxy Pattern
A structural design pattern that provides a substitute or a placeholder for
another object.
A proxy controls access to the original object, allowing you to perform
something either before or after the request gets through to the
original object.
1
UML class and sequence diagrams
2
Proxy Pattern
Why control the access to an object?
If you have a massive object that consumes a vast amount of
resources.
And you do not need it always.
3
Proxy Pattern
A credit card is a proxy for a bank account.
Both implement the same interface for making a payment.
A consumer does not need to carry loads of cash around.
4
Proxy Pattern
•Providing a surrogate or placeholder for another object to control
access to it.
•Using an extra level of indirection to support distributed, controlled,
or intelligent access.
•Add a wrapper and delegation to protect the real component from
undue complexity.
•Supporting resource-hungry objects, until they are actually
requested by the client.
5
Types of proxies
Remote proxy:
Representing the object located remotely. All that logic is encapsulated in
these proxies.
Virtual proxy:
These proxies initiate the operation on real objects and provide a default
result to the application. Once the real object is done, these proxies push
the actual data to the client where it has provided dummy data earlier.
Protection proxy:
If an application does not have access to some resource then such
proxies will talk to the objects in applications that have access to that
resource.
6
Proxy Pattern
The Proxy class has a reference
field that points to a service
object.
After the proxy finishes its
processing (e.g., lazy initialization,
logging, access control,
caching, etc.), it passes the
request to the service object.
Proxies can manage the full
lifecycle of their service objects.
7
Java Example
Proxy design pattern
• Create a "wrapper" for a remote, or expensive, or sensitive target
• Encapsulate the complexity/overhead of the target in the wrapper
• The client deals with the wrapper
• The wrapper delegates to the target
• To support plug-compatibility of wrapper and target, create an
interface
8
Implementation (virtual proxy)
ProxyImage is a proxy class to reduce memory of RealImage object loading.
ProxyPatternDemo, will use ProxyImage to get an Image object to load and
display as it needs.
https://www.tutorialspoint.com/design_pattern/proxy_pattern.htm
10
11
12
Proxy Pattern
One of the advantages of Proxy pattern is security.
This pattern avoids duplication of objects which might be huge size
and memory intensive. This in turn increases the performance of the
application.
The remote proxy also ensures about security by installing the local
code proxy (stub) in the client machine and then accessing the server
with help of the remote code.
13
Proxy Pattern
Pros
You can control the service object without clients knowing about it.
• You can manage the lifecycle of the service object when clients don’t care
about it.
• The proxy works even if the service object isn’t ready or is not available.
• Open/Closed Principle. You can introduce new proxies without changing
the service or clients.
Cons
• The code may become complicated since you need to introduce a lot of
new classes.
• The response from the service might get delayed.
14