Proxy Design Pattern

  • Proxy is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.

How Proxy Works in Real World (Go through the hyperlink)

Problem :

  • Why you want to control access to another object ?
    • The resource might be heavy or a native 3rd party library call
  • So the cost of accessing it hight
    • Hence you need to do some steps before and after the 3rd party call is completed like validation before and after it.

Solution :

  • The Proxy pattern suggests that you create a new proxy class with the same interface as an original service object. 
  • Then you update your app so that it passes the proxy object to all of the original object’s clients. 
  • Upon receiving a request from a client, the proxy creates a real service object and delegates all the work to it.
  • What is the benefit
    • If you need to execute something either before or after the primary logic of the class, the proxy lets you do this without changing that class. 
    • Since the proxy implements the same interface as the original class, it can be passed to any client that expects a real service object.
  • Applicable when
    • Lazy initialization : Instead of creating the object when the app launches, you can delay the object’s initialization to a time when it’s really needed.
    • Access control : The proxy can pass the request to the service object only if the client’s credentials match some criteria.
    • Local execution of a remote service (remote proxy).  : In this case, the proxy passes the client request over the network, handling all of the nasty details of working with the network.
    • Logging requests (logging proxy). : The proxy can log each request before passing it to the service.
    • Caching request results : The proxy can implement caching for recurring requests that always yield the same results. The proxy may use the parameters of requests as the cache keys.
    •  Smart reference : This is when you need to be able to dismiss a heavyweight object once there are no clients that use it.

Example

Types of proxy :

  • Remote Proxy – (DB Access)
  • Virtual Proxy – (Caching)
  • Protection Proxy – (Access Control)

Example :

Interface of Internet

package com.saket.demo.proxy;

public interface Internet
{
	public void connectTo(String serverhost) throws Exception;
}

RealInternet.java

package com.saket.demo.proxy;

public class RealInternet implements Internet
{
	@Override
	public void connectTo(String serverhost)
	{
		System.out.println("Connecting to "+ serverhost);
	}
}

ProxyInternet.java

package com.saket.demo.proxy;

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


public class ProxyInternet implements Internet
{
	private Internet internet = new RealInternet();
	private static List<String> bannedSites;
	
	static
	{
		bannedSites = new ArrayList<String>();
		bannedSites.add("abc.com");
		bannedSites.add("def.com");
		bannedSites.add("ijk.com");
		bannedSites.add("lnm.com");
	}
	
	@Override
	public void connectTo(String serverhost) throws Exception
	{
		if(bannedSites.contains(serverhost.toLowerCase()))
		{
			throw new Exception("Access Denied");
		}		
		internet.connectTo(serverhost);
	}
}

Client.java

package com.saket.demo.proxy;

public class Client
{
	public static void main (String[] args)
	{
		Internet internet = new ProxyInternet();
		try
		{
			internet.connectTo("geeksforgeeks.org");
			internet.connectTo("abc.com");
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
		}
	}
}

Reference :

Leave a Comment