This tutorial explains Proxy design pattern in java with class diagrams and example code.
Introduction – Proxy Design Pattern is a structural design pattern among the Gang Of Four(GOF)Article on GOF Patterns & their types Design Patterns. A structural design pattern deals with how the relationships between objects are realized to make the design better.
What is Proxy Design Pattern
Proxy pattern specifies a design where substitute or placeholder object is put in-place of the actual target object to control access to it. Client accesses the proxy object to work with the target object. There are many practical usages and applications of using a target object through a proxy. Lets have a look at the major ones.
Usages and Applications of Proxy Pattern
- Remote Proxy – Using a remote proxy, clients can access objects on a remote location as if they are co-located with them. In Java, using the java.rmi.Remote interface of RMI is an example of this.
- Virtual Proxy – A virtual proxy creates an instance of an expensive Object only on demand. I.e. it saves on resources by not creating an instance of an Object heavy on resources until it is needed.
- Protection Proxy – A protection proxy regulates access to the original object. Its similar to authroization i.e. object access is controlled based on access rights defined for that Object.
- Smart Reference – A smart reference proxy does additional actions when an object is accessed which typically include things like loading a persistent object into memory when its first referenced, locking of objects to avoid inconsistencies in data held by the object etc. It can also keep track of current usage of the real object and in case it is no longer used then a smart reference unloads the object from memory to load it back only when its needed.
Class Diagram for Proxy Design Pattern
Explanation of Class Diagram
RealSubjectis the class which contains the logic to be executed.
Proxyobject contains the instance of
RealSubjectand controls access to its
- Client cannot use a
RealSubjectinstance directly. It must obtain an instance of
Proxyfirst. However, this delegation of responsibility from
Proxyis transparent for
clientstill thinks that its working with
- To enable this substitution of
Proxyone more layer of abstraction exists above
RealSubject, i.e. the
Clientcontains a reference to this
- Subject reference in Client contains an instance of Proxy class (which is possible as both Proxy and RealSubject are children of Subject)
- When client invokes
Subjectwhich actually contains an object of
Proxygets the request from
Client, does some work on incoming request, such as authentication/loading of value/remote access initiatation etc and invokes the
RealSubject. This extra work which
Proxygets to do on the incoming request is one of the primary advantages of using the
Proxycan do the same processing in reverse also when it receives a response from
RealSubjectobject. After processing/massaging the response
Proxyreturns the final response to
- In the whole process Client thinks its using the
RealSubjectdirectly, however, in reality its the
Proxyobject which controls access to
Example in Java – Class Diagram
The Java class diagram above depicts proxy pattern implemented for a text file reader application where –
TextFileis an abstract class which is the base class for all text files.
TextFilehas 2 abstract methods –
getNameWithPath()which returns name of the file with system path to of the file appended, and
getFileContents()which returns the contents of the file as a FileInputStream instance.
TextFilealso has a static method for returning an instance to it called
getTextFileInstance(). This is where Proxy part kicks in as this method instead of returning a
RealTextFileinstance returns a
RealTextFileis a sub-class of
TextFileand it contains the actual contents of a file.
TextFileProxyis also a sub-class of
TextFile. As mentioned earlier, when a new TextFile instance is requested via the
TextFileProxyinstance is returned which just holds the file name & path String value. The actual instance of a file, i.e. a RealTextFile instance, is only created by a TextFileProxy when the Client wants to get the contents of the text file using
getFileContents()method. At this point, TextFileProxy creates a new instance of RealTextFile with the
fileNameWithPathvalue stored in it.TextFileProxy then assigns the RealTextFile instance to the class attribute
realTextFileand then returns the contents of the text file using
- If you take a look at the code in
main()method in Client class then initially a new TextFile instance is created using the static method
getTextFileInstance(). On printing the file name with path it gets printed from TextFileProxy instance.
- Next when the
textFile.getFileContents()is invoked, then internally RealTextFile’s instance is invoked as explained above and a FileInputStream object is returned.
- In the output –
- First the file’s path is printed from
- Then, the
FileInputStream‘s reference object’s information is printed when the
getFileContents()which in turn instantiates and invokes
- First the file’s path is printed from
In the above tutorial we looked at what is proxy design pattern with its common usages and applications. We then looked at the class diagram for a proxy design pattern & its explanation, followed by a Java example of proxy pattern with its class diagram and explanation. This concludes the tutorial on proxy design pattern.