JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

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:

1) A remote proxy provides a local representative for an object that resides in a different address space. This is what the "stub" code in RPC and CORBA provides.

2) A virtual proxy is a placeholder for "expensive to create" objects. The real object is only created when a client first requests/accesses the object.

3) A protective proxy controls access to a sensitive master object. The "surrogate" object checks that the caller has the access permissions required prior to forwarding the request.

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 {

	@Override
	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>();
		restrictedSites.add("jumbxyz.com");
		restrictedSites.add("testme.com");
		restrictedSites.add("adult-site.com");
		restrictedSites.add("bad-site.com");
	}
	
	@Override
	public void connectTo(String host) throws Exception {
		
		if(!restrictedSites.contains(host.toLowerCase())){
			internet.connectTo(host);
		}
		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 {
			intConn.connectTo("java2novice.com");
			intConn.connectTo("adult-site.com");
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

Output:
Connecting to java2novice.com
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
Default value of a local variables?
The local variables are not initialized to any default values. We should not use local variables with out initialization. Even the java compiler throws error.
Famous Quotations
I respect faith, but doubt is what gets you an education.
-- Wilson Mizner

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.