Proxy Design Pattern

In Proxy pattern, a proxy object represents a placeholder or surrogate which provides an interface to outer world to access the functionality of original object. A proxy object is simply means an object representing another object. Proxies are also known as handles, surrogates, and wrappers. This type of design pattern comes under structural design pattern.

Usage of Proxy Pattern

There are four situations in which the Proxy pattern is used.
  • Virtual proxy
    Suppose, you want to access a huge file from a database. Since, initialization of a database client is expensive operation, we will use proxy pattern to instantiate an instance of database client on first database request by client. After the first request, proxy will reuse the database client for any future requests by client instead of creating a new instance of database client everytime. This will reduce the duplication of object, reduce latency to access data from database and save memory.

  • Remote proxy
    A remote proxy of a remote resource (like a web services) provides a local interface of the remote resource at different address location. A client can use the interface provided by remote proxy or a remote resources to access the functionalities of a remove resource. Talking to the remote resource might involve serialization and deserialization of data, all such logic can be encapsulated in remote proxies and the client application need not worry about their implementation. Examples of remote proxies include a proxy of REST service or aws S3.

  • Smart Proxy
    A smart proxy can provide some additional functionalities to access or optimize the interation between client and resource like splitting a large request to access 100 images into 5 requests of 20 images each. Other uses of smart proxies include to provide additional security, to provide failure handling in case or any problem while accessing resource etc.

  • Protection proxy
    A protection proxy is used to enforce access control to a resource. It acts as an authorization layer to verify that whether client has access necessary access the appropriate resource or not. If client have appropriate access then it will forward the client's request to the resource else block the unauthourized client request from accessing the resource.

Advantages of Proxy Pattern

  • In proxy pattern, we provide interface of functionality supported by original object to outer world to hide the complexity of the original object.

  • Proxy provides an additional layer of protection to the original object from the outside world.

  • A local proxy code running on the client machine can enforce the constraints required by to access the server. It can perform some operations locally before making a remote call to server.

  • Proxy pattern increases the performance of the application by avoids creation or duplication of objects which might be complex and memory intensive.

Implementation of Proxy Design Pattern

In below mentioned example, we will create a proxy class called ImageManagerProxy which provides an interface to outer world to access the functionalities of ImageManager class.
First of all we will create an ImageServer interface and it's concrete implementation as ImageManager.

Proxy Design Pattern UML Diagram
  • void displayImage(String imageName) : Downloads image file and display it on screen.
  • void uploadImage(String imageName): Uploads image file to server.
public interface ImageServer {
    void displayImage(String imageName);
    void uploadImage(String imageName);
public class ImageManager implements ImageServer {

    public void displayImage(String imageName) {
        System.out.println("Downloading image file" + imageName);
        System.out.println("Displaying Image" + imageName);

    public void uploadImage(String imageName) {
        System.out.println("Uploading image file" + imageName);
ImageManagerProxy class implements the same interface ImageServer and provides access to functionalities of ImageManager to external world.
public class ImageManagerProxy implements ImageServer {
    private ImageManager imageManager;

    public void displayImage(String imageName) {
        if(imageManager == null) {
            imageManager = new ImageManager();

    public void uploadImage(String imageName) {
        System.out.println("Compressing image file" + imageName);
        if(imageManager == null) {
            imageManager = new ImageManager();
ProxyPatternExample will use ImageManagerProxy to invoke ImageManager instead of calling ImageManager directly.
public class ProxyPatternExample {
    public static void main(String[] args) {
        ImageManagerProxy imageManagerProxy = new ImageManagerProxy();
        // Upload image using proxy
        // Display image using roxy


Compressing image fileProfile_pic.jpg
Uploading image fileProfile_pic.jpg
Downloading image fileProfile_pic.jpg
Displaying ImageProfile_pic.jpg

Important Point about Proxy Pattern
Here are the differences between Proxy pattern and other related patterns.
  • A Proxy provides the same interfacte as original object.

  • An Adapter provides a different interface of the original object for compatibilty.

  • A Decorator provides an enhanced interface by adding for additional features on top of the features provided by original object.

Related Topics
Iterator Design Pattern
State Design Pattern
Bridge Design Pattern
Prototype Design Pattern
Memento Design Pattern
Filter Design Pattern
Singleton Design Pattern
Factory Design Pattern
Mediator Design Pattern
Observer Design Pattern
List of Design Patterns