Monday, May 6, 2019

Proxy Design Pattern Example


Proxy Design Pattern

The definition of Proxy pattern  provided by Gang of Four book on Design Patterns states:
"Provide a surrogate or placeholder for another object to control access to it."
  • Proxy design pattern is one of Structural Design Pattern.
  • This design pattern is used to create a surrogate or wrapper "in place of" another object.
  • We can modify the functionalities of an object without modifying the actual object’s behavior.  
  • The Surrogate or wrapper object is known as proxy of actual object. So we have access controls to actual object.

Q- When to use Proxy Design Pattern ?
  • We can use Proxy design pattern when there is a requirement to access control to an Object.
  • when there is a need for a reference to an Object.
  • If we want to add/modify the object’s behavior without affecting actual object’s behavior.  
There are 4 common situations we can use proxy design pattern.
  • Remote proxy:
  • Virtual proxy
  • Protection proxy:
  • Smart Proxy


CollegeInternet.java
package com.shubh.dp.example;

public interface CollegeInternet {
 public void getInternet();
}


CollegeInternetImpl
package com.shubh.dp.example;

public class CollegeInternetImpl implements CollegeInternet {
 private String department;

 public CollegeInternetImpl(String detpName) {
  this.department = detpName;
 }

 @Override
 public void getInternet() {
  System.out.println("Internet has been connected successfully for Department: " + department);
 }
}


InternetProxy.java
package com.shubh.dp.example;

import java.util.ArrayList;
import java.util.List;

public class InternetProxy implements CollegeInternet {
 private String department;
 private CollegeInternet realObject;
 private List proxyLock = new ArrayList();

 public InternetProxy(String department) {
  this.department = department;
 }

 @Override
 public void getInternet() {
  if (!getProxyLock().contains(department)) {
   realObject = new CollegeInternetImpl(department);
   realObject.getInternet();
  } else {
   System.out.println("Internet access not permitted to Department: " + department);
  }
 }

 /* Add the department you want to block */
 public List getProxyLock() {
  proxyLock.add("Math");
  proxyLock.add("Physics");
  proxyLock.add("Chemistry");
  // proxyLock.add("Biology");
  proxyLock.add("Psychology");
  proxyLock.add("Sociology");
  // proxyLock.add("Computer");
  // proxyLock.add("Accounts");
  return proxyLock;
 }
}


ProxyDesignPattern.java
package com.shubh.dp.example;

public class ProxyDesignPattern {
 public static void main(String[] args) {

  /* Access Internate without Proxy */
  // System.out.println("Access Internate without Proxy.");
  CollegeInternetImpl realObject = new CollegeInternetImpl("Math");
  realObject.getInternet();

  /* Access Internate using Proxy */
  // System.out.println("Access Internate using Proxy.");
  InternetProxy internet = new InternetProxy("Math");
  internet.getInternet();

  /* Access Internate using Proxy */
  // System.out.println("Access Internate using Proxy.");
  InternetProxy internetAcc = new InternetProxy("Accounts");
  internetAcc.getInternet();
 }
}

Output:

Internet has been connected successfully for Department: Math
Internet access not permitted to Department: Math
Internet has been connected successfully for Department: Accounts

No comments:

Post a Comment