Publish Your Article Here

Proxy Design Pattern in java

Proxy design pattern is very simple, but very effective. Proxy pattern provide a surrogate or placeholder for another object to control access to it.

Proxy design pattern allows us to create a wrapper class over real object. Wrapper class which is proxy, controls access to real object so in turn we can add extra functionalities to real object without changing real object's code. A very simple real life scenario is our office internet, which restricts few site access. The proxy first checks the host you are connecting to, if it is not part of restricted site list, then it connects to the real internet.

There are four common situations in which the Proxy pattern is applicable:




4) A smart proxy interposes additional actions when an object is accessed.

Here is the code for proxy pattern.

package com.java2novice.dp.proxy;

public interface Internet {

	public void connectTo(String host) throws Exception;

Real internet class

package com.java2novice.dp.proxy;

public class RealInternet implements Internet {

	public void connectTo(String host) {
		System.out.println("Connecting to "+host);

Proxy internet class

package com.java2novice.dp.proxy;

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

public class InternetProxy implements Internet {

	private Internet internet = new RealInternet();
	private static List<String> restrictedSites;
	static {
		restrictedSites = new ArrayList<String>();
	public void connectTo(String host) throws Exception {
		throw new Exception("Company restricted this site view");



proxy demo class

package com.java2novice.dp.proxy;

public class ProxyDemo {

	public static void main(String a[]){
		Internet intConn = new InternetProxy();
		try {
		} catch (Exception e) {

Connecting to
Company restricted this site view
<< Previous Program 

Java design pattern examples

  1. Factory Pattern
  2. Abstract Factory Pattern
  3. Builder Design Pattern
  4. Prototype Pattern
  5. Adapter Pattern
  6. Composite Pattern
  7. Proxy Pattern
Knowledge Centre
What is System.out in Java
Famous Quotations
-- Unknown Author

About Author



Most Visited Pages

Other Interesting Sites
