proxy design pattern overview

Proxy Design Pattern with examples

Introduction to Proxy Pattern

Proxy design pattern falls under the structural design pattern category and it is one of the most frequently used pattern in software development. This pattern helps to control the usage and access behaviours of connected resources. A separate object called ‘proxy’ helps to build the connection between the client and the original service object. Thus, the proxy will acts as a substitute or placeholder object in-place of the actual original object especially to control the access.  The nature of the original service object can be a remote, high cost in the creation or require secured access. Systems’ core behaviour will not change, only the structure and control of the inner components will modify to achieve better results.

GoF Definition,

‘Provide a surrogate or placeholder for another object to control access to it’

Pattern introduces a surrogate instead of the actual original object to interact with the external entities. You are accessing that original object through another object while preserving the entire system behaviour as it is.

Real-life Examples

We can find many examples of proxy pattern in the financial system. The credit card will act as an actual bank interface providing same functionalities. It will act as the money withdrawal at ATM, check balance, transfer money and several other facilities are fulfilled by the credit card proxy. In addition, a check can perform the money transferring functionality on behalf of an individual. It acts as an order for the bank to pay a specific amount of money from a person’s account to another person upon approval. Here, check acts as a proxy for a bunch of cash.

The Problem

This occurs on situations where the required object is not very easy to initialize or access. If that object resides in a remote library or it consumes a lot of system resources and client needs it only on specific conditions. Therefore, the initializing and maintaining such object is not the most efficient way for the system. If there are requirements of providing additional functionalities when accessing the actual service object, it causes another layer of workaround for the developer. Thus, the system needs to find a better solution to handle such situations.

Solution

Above problem can be solved by introducing the proxy: a substitute component in place of the expensive and secured actual object. This proxy will contain a similar interface as the original service object and when it receives the client request, the proxy object will create an instance of the original object and delegates via the response.

What is a Proxy?

A proxy is simply a substitute object for the original object. It also acts as a wrapper or agent object which, is called by the client to access the original object behind the scene. A proxy is a lightweight object that implements the same interface as the original actual object as well as controls the access to the actual object. It can include additional functionalities if required like caching when the operations on the actual object are resource extensive and assuring pre-conditions before invoking actual object operations.

 

proxy design pattern overview

Why we use a Proxy?

A proxy can perform different operations depending on the requirement. It can perform pre-processing like logging and filter for requests before handing over to the original object to accomplish certain preconditions. Likewise, proxies can perform post-processing prior to sending the results back to the requester. Proxies can be utilized when there is a need for overriding functionality. Especially, when there is a need for breaking licenses that are preventing breaking existing legacy systems, proxies will be a good option. Proxies are good candidates when there is a need of cashing mechanism for resource-intensive applications to reduce the traffic and cost on the network. Proxies can be used to endure the security operations of the systems when there are expensive and complex security concerns.

 Proxy Pattern Variations

There are several types of proxy pattern variations.

Remote Proxy

As the name suggests these proxies are working on different workspaces. That is actual original object exists in a remote area. A remote proxy acts as a local representative for the remote object, because of this reason client is unaware of the remoteness of the original object. The ‘stub’ object in Java RMI technology is a fine example of this pattern. The remote original object resides in a heap of a different JVM when the client requires getting the service of the original object, the stub will act as the proxy to interact and invoke methods of the remote object.

Virtual Proxy

This is a way of saving cost in an application. In this situation, the creation of the actual original object is expensive and memory and resource consumption is high. To avoid this barrier, application introduces the virtual proxy. The virtual proxy acts as a placeholder for the expensive original object and creates the real object of the original object only on demand. By this way, the virtual proxy can save the cashed actual object and reuse in future calls preventing object duplication and saving memory.

Protection Proxy

This proxy adds a layer of protection to the original object. In this case, the original object is protected by different access levels. When the client wants to access the original object, protection proxy will check the appropriate access level of the client prior to forwarding the request.

Smart Proxy

This proxy provides an additional layer of security. It performs additional actions to verify that the original object is safe from unwanted access and threats like accidental disposal/deletion of the original object, checking whether the original object is locked or not before accessing it and loading the persistent object into memory when it is first referenced.

Class Diagram of the Proxy Design Pattern

 

proxy design pattern class diagram

Components of the Proxy Design Pattern

Proxy

Controls access to the real subject. The proxy contains an instance of the real subject and provides an interface identical to the Subject in order to contact the real subject easily. There can be additional methods inside the proxy class to perform intermediate work.

Subject

This is the common interface for the real subject and the proxy. Therefore, both the proxy and the real subject implements the ‘Subject’ interface. The client only interacts with the proxy to access the real subject since the proxy can be used in place of the real subject at any moment.

RealSubject

This is the actual original object, which is accessed through the proxy. This could be a network connection, a file, a large object in memory or some other expensive or difficult to create the component

How does Proxy Pattern Works?

In this pattern client does not directly interact with the original object, instead, the client uses the proxy object to make the call to the original object. But the most important point is the client is not aware of the proxy and the proxy behaves as the original object for the client.

Example for Proxy Pattern

Let’s assume a very rare species of animal called ‘Sea Bear’ in a nature park. Not every park visitors will be allowed to visit the sea bear. Only authorized people like biologists, vets, and animal researchers will be able to visit them. To control the access of this rare species park has appointed a ‘Sea Bear Guard’. He will assess the details of the visitors and controls the sea bear visits as appropriate.

Let’s implement this situation in the proxy pattern and identify the related components.

SeaBearGuardProxy : This is the proxy class to the SeaBearOriginal class.

BearProtectInterface : This is the interface which is shared by both SearBearOriginal and SeaBearGuardProxy classes and contains the access controlling mechanism.

SeaBearOriginal : This is the actual original class which represents the rare animal species in the nature park

NatureParkVisitorClient: This  is the visitor class who comes to visit the sea bear

Steps to Implement the Proxy Pattern

 

  1. Identify the actual original object, which needs to wrap through a proxy.
  2. Identify what steps needs to perform to control the access to the original object.
  3. Create a suitable interface that can be implemented by both the original object and the proxy.
  4. Create the proxy with access controlling mechanism and any other additional functionalities that support the intention of the proxy.
  5. Create the client object and access the original object via the proxy

Code Samples for the Example

 

BearProtectInterface.java

SeaBearGuardProxy.java

SeaBearOriginal.java

NatureParkVisitorClient.java

Sequence Diagram for the Nature Park Example

 

proxy design pattern sequence diagram

When using the Proxy Pattern

  • When the original object creation is expensive
  • When the original object exists in a remote environment
  • When there are restricted security controls on the original object
  • When there is need of performing additional operations before and after the creation of the original object
  • When the original object creation is on demand or system requires delays when loading certain resource
  • When the original object resides in a legacy system or 3rd party library

T Tak

Hey ! I'm Tak. I'm a software developer since 2008 and been programming since I was 17 years, started with pascal. I love programming, teaching and building stuff on web.

One thought to “Proxy Design Pattern with examples”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.