Proxy
[[Design Patterns — 6]]
- Concept of Proxy Design Pattern
- Characteristics of Proxy Design Pattern
- Class Diagram for Proxy Design Pattern
- Collaboration Diagram for Proxy Design Pattern
- Example Code to demonstrate the use of Proxy Design Pattern
Concept of Proxy Design Pattern:
The idea of Proxy Design is to make the client use a service through a proxy. The proxy should delegate the operations to the actual concrete service implementation. The proxy should implement policies around what should be done when operations are asked to be performed. The client should be independent of the concrete implementations of the service. The client code does not change irrespective of whether a proxy is used or not. The client is unaware of whether a proxy is being used or not.
Characteristics of Proxy Design Pattern
- Can be used to delay the execution time of an operation.
- Remote Proxy: Creates an illusion that an object on a remote machine is just an object on the same machine.
- Protection Proxy: Control the manner in which the concrete service implementing classes can be accessed through a proxy based on certain policies.
- Virtual Proxy: Creates an illusion that the service providing object is available early when in fact it is just a proxy object. The actual underlying service implementing object could be expensive to create and hence is initialised lazily.
Get Prince Jha’s stories in your inbox
Join Medium for free to get updates from this writer.
Class Design Diagram:
- Client: class in this role wants to use a service. It depends on the ServiceIF interface to obtain the services.
- ServiceIF: interface in this role represents the operations that a service should provide.
- ServiceA, ServiceB: classes in these roles are concrete service providing classes.
- ServiceProxy: class in this role represents a proxy. The proxy class delegates operations to other service providing classes using the serviceIF interface. The ServiceProxy class also implements the ServiceIF interface itself so that clients can access the class as if were some service providing class. The real use of ServiceProxy class is to implement certain policies regarding under what circumstances the operations should be delegated in which manner etc.
Collaboration Diagram for Proxy Design Pattern:
Example Code to demonstrate the use of Proxy Design Pattern:
Let us design a proxy which delays the add method on List by N seconds. We will make the same thread wait for at least N seconds before completing the add operation.
import java.util.*;
import java.util.concurrent.TimeUnit;
class DelayList<E> implements List<E> {
private static int DEFAULT_DELAY_TIME = 2;
private List<E> associatedList;
private int delayTimeInSeconds;
public DelayList(List<E> theList) {
associatedList = theList;
delayTimeInSeconds = DEFAULT_DELAY_TIME;
}
public DelayList(List<E> theList, int numberOfSeconds) {
associatedList = theList;
delayTimeInSeconds = numberOfSeconds;
}
public List<E> subList(int fromIndex, int toIndex) {
return new DelayList(associatedList.subList(fromIndex, toIndex), delayTimeInSeconds);
}
public ListIterator<E> listIterator(int index) {
return associatedList.listIterator(index);
}
public ListIterator<E> listIterator() {
return associatedList.listIterator(0);
}
public int lastIndexOf(Object targetObj) {
return associatedList.lastIndexOf(targetObj);
}
public int indexOf(Object targetObj) {
return associatedList.indexOf(targetObj);
}
public E remove(int index) {
return associatedList.remove(index);
}
public boolean add(E obj) {
try {
TimeUnit.SECONDS.sleep(delayTimeInSeconds);
return associatedList.add(obj);
} catch(InterruptedException error) {
System.out.println("Interrupted thread when blocked/waiting " + error.getMessage());
return false;
}
}
public void add(int index, E obj) {
associatedList.add(index, obj);
}
public E set(int index, E obj) {
return associatedList.set(index, obj);
}
public E get(int index) {
return associatedList.get(index);
}
public void clear() {
associatedList.clear();
}
public boolean retainAll(Collection<?> coll) {
return associatedList.retainAll(coll);
}
public boolean removeAll(Collection<?> coll) {
return associatedList.removeAll(coll);
}
public boolean addAll(int index, Collection<? extends E> coll) {
return associatedList.addAll(index, coll);
}
public boolean addAll(Collection<? extends E> coll) {
return associatedList.addAll(coll);
}
public boolean remove(Object obj) {
return associatedList.remove(obj);
}
public Object[] toArray() {
return associatedList.toArray();
}
public <T> T[] toArray(T[] obj) {
return associatedList.toArray(obj);
}
public Iterator iterator() {
return associatedList.iterator();
}
public boolean contains(Object obj) {
return associatedList.contains(obj);
}
public boolean isEmpty() {
return associatedList.isEmpty();
}
public int size() {
return associatedList.size();
}
public boolean containsAll(Collection<?> coll) {
return associatedList.containsAll(coll);
}
}
public class Client {
public static void main(String args[]) {
List<Integer> arr = new DelayList<>(new ArrayList<>());
System.out.println("Before a few add operations");
arr.add(5000);
arr.add(20000);
System.out.println("After a few add operations");
return;
}
}Of course writing a Wrapper Class and using the delegation technique involves writing lots of code.








