Table of Contents
When to use it:
- A remote proxy provides a local representative for an object in a different address space.Providing interface for remote resources such as web service or REST resources.
- A virtual proxy creates expensive object on demand.
- A protection proxy controls access to the original object.Protection proxies are useful when objects should have different access rights.
- A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.
- Adding a thread-safe feature to an existing class without changing the existing class’s code.
UML diagram for proxy design pattern:
Elements:
- Proxy
- maintains a reference that lets the proxy access the real subject.Proxy may refer to a subject if the RealSubject and Subject interface are the same.
- provides an interface identical to Subject’s so that proxy can be substituted for the real subject.
- controls access to the real subject and may be responsible for creating and deleting it.
- Subject
- defines the common interface for RealSubject and proxy so that a Proxy can be used anywhere a RealSubject is expected.
- RealSubject
- defines the real object that proxy represents.Â
WorkFlow:
Proxy forwards request to RealSubject when appropriate ,depending on the kind of proxy.
Example:
Let’s see an example.I am taking example of protection proxy. In our example, we have a folder in which you can perform various operation such as copy,paste file or subfolder.In OOP terms,we have IFolder interface and Folder class which provides performOperatons() method, and these are the existing class and interface that we cannot change. We want to further specify that only user with authorization can access it and perform operations such as cut or copy files and sub folder.
UML diagram for example:
Comparing with above generic elements,we have
- FolderProxy(Proxy)
- Folder(RealSubject)
- IFolder(Subject)
Java codes for above classes:
IFolder.java (Subject):
1 2 3 4 5 6 7 8 |
package org.arpit.javapostsforlearning.designpatterns; public interface IFolder { Â Â Â public void performOperations(); } |
Following class is our realSubject class.Lets say we can not change it but we want to provide authorization on it.
Folder.java (RealSubject):
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package org.arpit.javapostsforlearning.designpatterns; public class Folder implements IFolder{ public void performOperations() { // access folder and perform various operations like copy or cut files System.out.println("Performing operation on folder"); } } |
Following class provides authorization to Folder class.It checks for userName and password and if matched then only it gives access to folder.
FolderProxy.java(Proxy):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
package org.arpit.javapostsforlearning.designpatterns; public class FolderProxy implements IFolder{ Folder folder; User user; public FolderProxy(User user) { this.user = user; } public void performOperations() { if(user.getUserName().equalsIgnoreCase("arpit") && user.getPassword().equalsIgnoreCase("arpit")) { folder=new Folder(); folder.performOperations(); } else { System.out.println("You don't have access to this folder"); } } } |
User.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package org.arpit.javapostsforlearning.designpatterns; public class User { String userName; String password; public User(String userName, String password) { this.userName = userName; this.password = password; } public String getUserName() { return userName; } public String getPassword() { return password; } } |
ProxyDesignPatternMain.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package org.arpit.javapostsforlearning.designpatterns; public class ProxyDesignPatternMain { public static void main(String[] args) { //When you click on folder,Lets say a GUI form will ask for userName and password. //and this GUI will create this user object // If we give correct userName and password User user=new User("arpit","arpit"); FolderProxy folderProxy=new FolderProxy(user); System.out.println("When userName and password are correct:"); folderProxy.performOperations(); System.out.println("**************************************"); // if we give wrong userName and Password User userWrong=new User("abc","abc"); FolderProxy folderProxyWrong=new FolderProxy(userWrong); System.out.println("When userName and password are incorrect:"); folderProxyWrong.performOperations(); } } |
Output:
1 2 3 4 5 6 7 |
When userName and password are correct: Performing operation on folder ************************************** When userName and password are incorrect: You don't have access to this folder |
So without changing Folder class,we have provide authentication to folder with the help of FolderProxy class.
Source code:
1 2 3 |
<b>Source:</b><a href="https://dl.dropbox.com/s/7cbol0f26yboxyy/ProxyDesignPatternExample.7z" target="_blank">Download</a> |