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 {

    public String display() {

        String retString = "";

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

        return retString;

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

        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() {


    public String display() {
        if(realImg.pic == null) {
            realImg = new RealImage();
        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();

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

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

        Image niceImage = new ImageProxy();

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


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