Learnitweb

Proxy design pattern – Java

1. Introduction

Proxy means “power or authority that is given to allow an entity to act for someone else”. Proxy objects are used as surrogates and handlers for original objects. The objective of a proxy object is to control the creation of and access to the real object it represents. A common use of a proxy object is to defer the cost of instantiating of an expensive object until it is actually needed by clients.

The GoF definition of Proxy Pattern is:

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

Proxy Pattern comes under category of Structural Design Pattern.

2. Real-life example

  • When someone invites your father to attend a function, you father may ask you to attend the function in place of him. You are working as proxy for your father.
  • When a student mimic some other student’s voice and call out during roll call, the student is working as a proxy for his friend.

3. Programming example

  • Proxies are used in networking. Proxy adds another layer before accessing the actual network.
  • Proxies are used in object creation when the actual object is expensive to create.
  • A bank ATM is a proxy of banking services.
  • Hibernate uses a proxy object to implement lazy load entities.

4. Usage scenarios of proxy pattern

  • Remote proxy: A local object represents a remote object. The local and remote objects have different address space. The local object is a proxy for the remote object. This pattern is used in distributed services where method invocation on the local object results in remote method invocation on the remote object.
  • Virtual proxy: When the actual object creation is expensive, a skeleton of the object can be used. The skeleton acts as a proxy. When the request is actually made to get the complete object only then the complete object is created.
  • Protection proxy: A protection proxy is used to control access to a resource based on access rights. Extra security can be provided with an extra layer. This is called as protection proxy.

5. Example

In this example, we’ll create a simple proxy object to mimic the proxy network. The proxy first checks if the website which you are trying to access is in the list of restricted websites. If the website is not in the list of restricted websites, only then it will allow the access.

5.1 Implementation

IInternet.java

package com.learnitweb;

public interface IInternet {
	public void connect(String serverhost) throws Exception;
}

ProxyInternet.java

package com.learnitweb;

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

public class ProxyInternet implements IInternet {
	private IInternet internet = new RealInternet();
	private static List<String> restrictedSites;

	static {
		restrictedSites = new ArrayList<String>();
		restrictedSites.add("http://restricted1.com");
		restrictedSites.add("http://restricted2.com");
		restrictedSites.add("http://restricted3.com");
		restrictedSites.add("http://restricted4.com");
	}

	@Override
	public void connect(String address) throws Exception {
		if (restrictedSites.contains(address.toLowerCase())) {
			throw new Exception("Access Denied to " + address);
		}

		internet.connect(address);
	}
}

RealInternet.java

package com.learnitweb;

public class RealInternet implements IInternet {

	@Override
	public void connect(String address) throws Exception {
		System.out.println("Connecting to address: " + address);
	}
}

Client.java

package com.learnitweb;

public class Client {
	public static void main(String[] args) {
		IInternet internet = new ProxyInternet();
		try {
			internet.connect("https://learnitweb.com");
			internet.connect("http://restricted1.com");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

Output

Connecting to address: https://learnitweb.com
Access Denied to http://restricted1.com