This post gives a brief overview about the Proxy Pattern. The post is part of a series about software design patterns and their UML representations with the help of PlantUML.

The article aims at providing a very short description of the general idea of the pattern in the first part. This also involves a descriptive UML diagram. Then, the second part provides the PlantUML code for the diagram so that these posts can also be used as a source of design patterns in PlantUML syntax.

What is the Proxy Pattern?

According to Wikipedia, a proxy, in its most general form, is a class functioning as an interface to something else. The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply be forwarding to the real object, or can provide additional logic.

What problems can the Proxy design pattern solve?

  • The access to an object should be controlled.
  • Additional functionality should be provided when accessing an object.

When accessing sensitive objects, for example, it should be possible to check that clients have the needed access rights.

What solution does the Proxy design pattern describe?

Define a separate Proxy object that

  • can be used as substitute for another object (Subject) and
  • implements additional functionality to control the access to this subject.

This enables to work through a Proxy object to perform additional functionality when accessing a subject. For example, to check the access rights of clients accessing a sensitive object.
To act as substitute for a subject, a proxy must implement the Subject interface. Clients can’t tell whether they work with a subject or its proxy.

UML Diagram

The following diagram shows the Proxy Pattern in UML notation. It is based on the corresponding chapter in the book “Head First Design Patterns“:

PlantUML Syntax:</p>
<p>@startuml<br />
interface Subject {<br />
{abstract} request()<br />
}<br />
together {<br />
class RealSubject<br />
class Proxy<br />
}</p>
<p>RealSubject : request()<br />
Proxy : request()</p>
<p>Subject <|.. RealSubject<br />
Subject <|.. Proxy<br />
RealSubject <- Proxy : subject</p>
<p>note bottom of RealSubject<br />
The RealSubject is<br />
usually the object<br />
that does most<br />
of the real work;<br />
the Proxy controls<br />
access to it.<br />
end note</p>
<p>note “The Proxy often instantiates\nor handles the creation of\nthe RealSubject.” as n1</p>
<p>RealSubject .. n1<br />
Proxy .. n1</p>
<p>note right of Subject<br />
Both the Proxy and the<br />
RealSubject implement the<br />
Subject interface. This<br />
allows any client to treat<br />
the proxy just like the<br />
RealSubject.<br />
end note<br />
@enduml</p>
<p>

PlantUML Sources

PlantUML is a tool allowing users to create UML diagrams from a plain text language. Here are the PlantUML sources for the above software pattern:

@startuml
interface Subject {
{abstract} request()
}
together {
class RealSubject
class Proxy
}

RealSubject : request()
Proxy : request()

Subject <|.. RealSubject
Subject <|.. Proxy
RealSubject <- Proxy : subject
@enduml

Other Design Patterns

In another article you find information about how to put together a single-side web application using PlantUML.

[display-posts category=”Software Design Patterns in PlantUML” include_excerpt=”true” excerpt_length=”20″ excerpt_more=”… Read More” excerpt_more_link=”true” image_size=”thumbnail” wrapper=”div”]