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
What is fail-fast in java?
A fail-fast system is nothing but immediately report any failure that is likely to lead to failure. When a problem occurs, a fail-fast system fails immediately. In Java, we can find this behavior with iterators. Incase, you have called iterator on a collection object, and another thread tries to modify the collection object, then concurrent modification exception will be thrown. This is called fail-fast.
Famous Quotations
It’s not that I’m so smart, it’s just that I stay with problems longer.
-- Albert Einstein

About Author

Most Visited Pages

Other Interesting Sites

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