Software Design Patterns: Proxy

In today's world everyone came across a proxy server. As in the world of the internet a Proxy in Software Design Patterns is very similar to the Proxy Server. It's a substitute for the RealSubject. The Client uses the Subject in his work, and all you work with as a client, is the Interface of the Subject. But you use a Proxy as a "Middleman". This serves different purposes:

  • Providing a surrogate/placeholder for the real subject
  • Adding additional loading/unloading logic to the RealSubject
  • Adding additional functionality that is hidden for the client

The Proxy belong to the Structural Patterns.

But first lets look at the UML Class diagram:

Software Design Patterns: UML of a Proxy Pattern
  • The Client only uses method calls defined in "Subject"
  • This way the client can change if he wants to use the RealSubject or the Proxy at runtime

It will come clearer if we use an real world example.

Java Example

In the following example we'll use an Image, RealImage and ImageProxy.

Software Design Patterns: Proxy Example UML

Image can also be an Interface or an Abstract Class, depends on your implementation.

First we have our Image:

public interface Image {
    String display();
}

As I said before this one could be an Interface, Abstract Class depending on your implementation.

Then our RealImage:

public class RealImage implements Image {

    @Override
    public String display() {

        String retString = "";

        if(pic != null)
            retString += pic;
        else
            retString += "[pic not loaded]";

        return retString;
    }

    public void loadImage() {
        System.out.print("pic loading ...");
        for (int i = 0; i < 5; i++) {
            try {
                Thread.sleep(250);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.print(".");

        }
        System.out.println("");
        pic = "[*imagine* nice picture]";
    }

    String pic = null;

}

It has a fake loadImage() method ;) and it overrides the display() method of Image.

And of course the ImageProxy:

public class ImageProxy implements Image {

    public ImageProxy() {

    }

    @Override
    public String display() {
        if(realImg.pic == null) {
            realImg = new RealImage();
            realImg.loadImage();
        }
        return realImg.display();
    }

    RealImage realImg = null;

}

And this is how the "Client" would use it:


        //maybe this way without proxy:
        System.out.println("~~ without proxy ~~");
        RealImage normalImage = new RealImage();
        normalImage.loadImage();
        System.out.println(normalImage.display());

        //if the client forgets to load:
        System.out.println("~~ forgetting to load ~~");
        normalImage = new RealImage();
        System.out.println(normalImage.display());


        //with proxy:
        System.out.println("~~ with proxy ~~");

        Image niceImage = new ImageProxy();
        System.out.println(niceImage.display());

        //displaying it again:
        System.out.println("~~ display again ~~");

        System.out.println(niceImage.display());

The output looks like this:

~~ without proxy ~~
pic loading ........
[*imagine* nice picture]
~~ forgetting to load ~~
[pic not loaded]
~~ with proxy ~~
pic loading ........
[*imagine* nice picture]
~~ display again ~~
[*imagine* nice picture]

Proxy - Sequence Diagram

Software Design Pattern: Proxy Sequence Diagram


Book recommendation

Design Patterns - Elements of Reusable Object-Orientated Software