Proxy Design Pattern

Last modified on August 1st, 2014 by Joe.

“Provide a surrogate or placeholder for another object to control access to it” is the intent provided by GoF.

Proxy means ‘in place of’. In attendance roll call, we give proxy for our friends in college right? ‘Representing’ or ‘in place of’ or ‘on behalf of’ are literal meanings of proxy and that directly explains proxy design pattern. It is one of the simplest and straight forward design pattern.

Proxy design pattern gets second rank in popularity in interviews. Guess who gets the first rank? none other than singleton design pattern.

Possible Usage Scenarios

Proxy Design Pattern Example

Remote Proxy:

Sometime back I wrote an article on A helloworld for Soap Web Service. A part of it contains implementation of proxy design pattern. The client has the stub files generated which acts as a proxy for the classes in server side.

Java’s Support for Proxy Design Pattern

From JDK 1.3 java has direct support for implementing proxy design pattern. We need not worry on mainting the reference and object creation. Java provides us the needed utilities. Following example implementation explains on how to use java’s api for proxy design pattern.

package com.javapapers.designpattern.proxy;

public interface Animal {

	public void getSound();

package com.javapapers.designpattern.proxy;

public class Lion implements Animal {

	public void getSound() {

package com.javapapers.designpattern.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class AnimalInvocationHandler implements InvocationHandler {
	public AnimalInvocationHandler(Object realSubject) {
		this.realSubject = realSubject;

	public Object invoke(Object proxy, Method m, Object[] args) {
		Object result = null;
		try {
			result = m.invoke(realSubject, args);
		} catch (Exception ex) {
		return result;

	private Object realSubject = null;
package com.javapapers.designpattern.proxy;

import java.lang.reflect.Proxy;

public class ProxyExample {

	public static void main(String[] args) {
		Animal realSubject = new Lion();
		Animal proxy = (Animal) Proxy.newProxyInstance(realSubject.getClass()
				.getClassLoader(), realSubject.getClass().getInterfaces(),
				new AnimalInvocationHandler(realSubject));

Download Proxy Example Source

Important Points

Proxy Design Pattern Usage in Java API

Adapter vs Proxy Design Pattern

Adapter design pattern provides a different interface from the real object and enables the client to use it to interact with the real object. But, proxy design pattern provides the same interface as in the real object.

Decorator vs Proxy Design Patter

Decorator design pattern adds behaviour at runtime to the real object. But, Proxy does not change the behaviour instead it controls the behaviour.

Comments on "Proxy Design Pattern"

  1. Aman says:

    Really useful bolg for java developers.

  2. Mohan says:

    Your are simple and superb ……

  3. samir says:

    Really very helpful for me ,Can you post Java Refrences like weak refrences,soft refrences,Strong refrences,phenotne refrences

    Can you make it simple for me i tried hard and there are blog but cant satisfy…


  4. Anonymous says:

    Great Explanation!!

  5. Raju says:

    Great Explanation Joe, this blog is Very very help ful to me…..,

  6. Martin says:

    Thanks, but this is not a proxy. This is a dynamic proxy.

  7. Arun Reddy says:

    its really appreciating thing , bcz of very useful for beginners like me

  8. Ramachandran says:

    Simple explanation …. good sir.

  9. Amitabha Roy says:

    Hi Joe,
    Could you please consider adding one heading “Prototype vs Proxy Design Pattern” at the end of this article?


  10. Murali says:

    Good one

  11. Anonymous says:

    HI Joe,
    What happened no updates from the last 20days. your site is very awesome. i am adaily follower of u r site

  12. Anonymous says:

    Hi Joe

    Could you please provide some insight regarding performance issues in Java.

    May God Bless you!!
    Thanks in Advance.

    Prasad Varikuti

  13. Srijith says:

    Good one

  14. LD says:

    Wonderful !!
    THis is what I was trying to understand !
    Great article man!!

  15. Andrey says:

    Thank you, lot of complicated topics expressed in simple and understandable terms. Thanks again.

  16. Anil says:

    Nice Article

  17. Ebe says:

    the easiest and most logical examples on design patterns… suprisingly how much we use without knowing theyre actually design patterns…

    amazing, thanks…

  18. ragi says:


  19. Anonymous says:

    Just superb.. nice example

  20. Anonymous says:

    you are my proxy :)

  21. Anonymous says:

    the best explanation of Design patters. thanks so much for the help

  22. Anonymous says:

    very very neat and good …. ! thankyou….

  23. Dipak says:

    Very good example

  24. Anonymous says:

    really usefull

  25. Amir Iqbal says:

    Good one. Very informative

    Keep Posting!

    Amir Iqbal

  26. Satya Dev says:

    This web site is really very good and it gives you understanding of concepts that you will never forget.

  27. Vgopalkr says:

    Hi joe,

    You are the man!!!

    Simple and superb!

  28. Kalpesh says:

    very nice

  29. abc says:

    Nice article

  30. Ravi.M says:

    Very Informative.

  31. Sushil Saini says:

    I have read many java and design pattern related articles from your blog.
    Articles on your blog are very good and very simple.

    Thanks for all this great work !!

  32. yugandhar says:

    Excellent blog

  33. Hari Mohan says:

    Very Nice article…enjoyed reading it ! and examples are lively !!!! good choice of words !!!!!

  34. Steve says:

    Very good illustration. Thank you.

  35. Amit Tayal says:

    Sorry but I was not able to understand it fully :(

  36. Anonymous says:


  37. Thirupathi says:

    simply super boss

  38. fatemeh says:

    very good…

  39. Java designing code says:

    Really informative post .
    Composite design pattern is based on creating a tree structure in such a way that an individual leaf of the tree can be treated just like entire tree composition.

  40. Bubai says:

    I have one question regarding the proxy creation. The issue is we are doing

    // creating the real object
    Animal realSubject = new Lion();
    // creating the proxy object using the real object
    Animal proxy = (Animal) Proxy.newProxyInstance(realSubject.getClass()
    .getClassLoader(), realSubject.getClass().getInterfaces(),
    new AnimalInvocationHandler(realSubject));

    So my question is how it is different from calling the method directly by realSubject.getSound();, I mean to say how we are getting benefit by creating the proxy as proxy seems to be a very lightweight object? Also if you see the hashcode of proxy object and realSubject, both of them are giving same hashcode.

Comments are closed for "Proxy Design Pattern".