opencodez

Java Structural Design Patterns – Proxy Pattern

Do you remember signing your class attendance sheet on behalf of your friend (well of course your teacher doesnxt have to know :)). That is called Proxy. It means xin place ofx or xon behalfx.  The Proxy Pattern comes under Structural Design Pattern. Proxy pattern is very simple to understand; in this pattern mainly 2 classes are used real class and proxy class. A proxy class is used to access real class object. A proxy class which acts as a intermediary for requests coming from client and provides the control for accessing the target (real) object. We can say that proxy class provides the security to the original object from the outside world. Proxy pattern is also called as Surrogate or Placeholder.

In your application if you observe below points then you can use Proxy Pattern.

xHere are ALL other Java Design Patterns, explained in detail with examplesx

Proxy Pattern by Example

For simple understanding we will take a day to day life example, where you try to access a particular website and once you access you get a message that, the site is banned by your organization network. We will try to simulate something like that.

Lets define a small interface Network

public interface Network {
	public void connect(String url);
}

Later we will define two concrete implementation of this interface. One will be RealNetwork, which is core implementation and other will be its Proxy.

public class RealNetwork implements Network {

	@Override
	public void connect(String url) {
		System.out.println("Connected to .." + url);
	}

}

You can see that, the Real implementation do not have any restrictions of any sort. Lets see proxy.

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

public class ProxyNetwork implements Network {

	private Network network;

	public ProxyNetwork() {
		this.network = new RealNetwork();
	}

	public Network getNetwork() {
		return this.network;
	}

	@Override
	public void connect(String url) {
		if(!isConnectionBanned(url)) {
			this.network.connect(url);
		} else {
			System.out.println("Connection to " + url + " is prohibited!!!");
		}
	}

	public boolean isConnectionBanned(String url) {

		ListxStringx banned = new ArrayListxStringx();
		banned.add("abc.com");
		banned.add("xyz.com");

		if (banned.contains(url)) {
			return true;
		}

		return false;
	}

}

Now, as its Proxy, it has to do some checks before allowing the connection. It does that by checking the requested url in predefined site list and then decides on connection.

Running the example

Here is our demo client-

public class ProxyDemo {

	public static void main(String[] args) {

		Network proxy = new ProxyNetwork();
		proxy.connect("abc.com");
		
		proxy.connect("bbc.com");
		
		System.out.println("");
		System.out.println("By passing Proxy.. using Real class");
		
		Network real = new RealNetwork();
		real.connect("abc.com");
	}

}

Output

Connection to abc.com is prohibited!!!
Connected to ..bbc.com

By passing Proxy.. using Real class
Connected to ..abc.com

Drawback

With this pattern we are introducing another layer of abstraction which may be an issue if the Real interface is being used by some clients (as shown in above output). This will cause disparate behaviour as same functionality is being access using different means.

Conclusion

In proxy pattern even if the client would use the proxy object it is similar to using like a real object, because both the classes implement the same interface. So in Proxy pattern it provides controlled access to objects and object will get created with original interface to represents its functionality to outer world.

xHere are ALL other Java Design Patterns, explained in detail with examplesx