An Introduction to proxy design pattern in java
Posted By : Pavan Kumar | 21-Dec-2018
Design Pattern:-
We have 3 types of design patterns.
- Creational design pattern.
Behavioural design pattern.- Structural design pattern.
In this blog, I will show you how we can use the proxy design pattern in java.
The proxy design pattern comes under the structural design pattern category.
Proxy means 'on behalf of' are literal meanings of proxy and that directly explains Proxy Design Pattern.
Proxies are referred to as surrogates, handles, and wrappers. they're closely connected in structure, however not purpose, to Adapters and designers.
A real world example will be a cheque or card could be a proxy for what's in our bank account. It can be utilized in place of money, and provides a means of accessing that money when needed. And that’s exactly what the Proxy pattern will do – “Controls and manage access to the object they're protecting“.
When to use this pattern?
Proxy pattern is used when we need to create a wrapper to cover the original object’s complexity from the client.
Let's take a look on example.
A very simple real world situation is like college internet, that restricts few web site access. The proxy initial checks the host you're connecting to, if it's not a part of restricted web site list, then it connects to the real internet. This example is based on Protection proxies.
Lets see how it works :
First of all we will create an Internet interface
Internet.java
package com.pavan.demo.proxy;
public interface Internet
{
public void connectTo(String serverhost) throws Exception;
}
Now, we will create a class RealInternet class which will implements Internet Interface
RealInternet.java
package com.pavan.demo.proxy;
public class RealInternet implements Internet
{
@Override
public void connectTo(String serverhost)
{
System.out.println("Connecting to "+ serverhost);
}
}
Now, we are going to create a proxy class as ProxyInternet which will aslo implements Internet Interface
ProxyInternet.java
package com.pavan.demo.proxy;
import java.util.ArrayList;
import java.util.List;
public class ProxyInternet implements Internet
{
private Internet internet = new RealInternet();
private static List bannedSites;
static
{
bannedSites = new ArrayList();
bannedSites.add("abc.com");
bannedSites.add("def.com");
bannedSites.add("ijk.com");
bannedSites.add("lnm.com");
}
@Override
public void connectTo(String serverhost) throws Exception
{
if(bannedSites.contains(serverhost.toLowerCase()))
{
throw new Exception("Access Denied");
}
internet.connectTo(serverhost);
}
}
Now , we will create a client class to test
Client.java
package com.pavan.demo.proxy;
public class Client
{
public static void main (String[] args)
{
Internet internet = new ProxyInternet();
try
{
internet.connectTo("oodlestechnologies.com");
internet.connectTo("abc.com");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
}
As abc.com is mentioned in the banned sites, So the
Output :
Connecting to oodlestechnologies.com
Access Denied
Drawbacks of proxy pattern:
This pattern introduces another layer of abstraction that generally is also a problem if the RealSubject code is accessed by a client directly and a few of them would possibly access the Proxy classes. This would possibly cause disparate behaviour.
Thanks.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Pavan Kumar
Pavan is a bright Java developer. He is a learner by heart and has a passion and profile to adapt various technologies.