Click here to Skip to main content
15,879,348 members
Articles / Programming Languages / Java
Tip/Trick

Proxy Design Pattern in Java

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
1 Oct 2012CPOL3 min read 37K   197   4   1
It provides proxy design pattern in detail with example

Introduction

Proxy design pattern allows you to create a wrapper class over real object. Wrapper class which is proxy, controls access to real object so in turn you can add extra functionalities to real object without changing real object's code.

As described by GoF:

"Provide a surrogate or placeholder for another object to control access over it."

Real life example may be proxy server used in IT companies for internet access.It blocks access to social networking sites like Facebook, Twitter and other content which is not work related.

When To Use It

Proxy is required whenever there is need for more sophisticated or versatile reference to an object than a simple pointer. Here are some situations when proxy pattern is applicable.

  1. 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.
  2. A virtual proxy creates expensive object on demand.
  3. A protection proxy controls access to the original object. Protection proxies are useful when objects should have different access rights.
  4. A smart reference is a replacement for a bare pointer that performs additional actions when an object is accessed.
  5. 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 look at an example. I am taking an 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 performOperations() 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)
Java
package org.arpit.javapostsforlearning.designpatterns;

public interface IFolder {

    public void performOperations();
}

The following class is our realSubject class. Let's say we cannot change it but we want to provide authorization on it.

Folder.java (RealSubject)

Java
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");
 }

}

The 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)
Java
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
Java
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
Java
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

Java
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 provided authentication to folder with the help of FolderProxy class.

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
I am a java developer and blogger.Love to connect with people interested in java,programming or blogging.You can visit my blog at http://java2blog.com/

Comments and Discussions

 
GeneralVery Good Example Pin
Member 1052489314-Jan-14 20:54
Member 1052489314-Jan-14 20:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.