Let's consider a person named John. He's trying to get 500 Rupees from his account. He has an account in XYZ bank. He would have two options to withdraw 500 rupees from brown.
First Approach:
The first approach is visiting the bank. In the first approach, what he would do is, he would try to visit the bank then he would fill the challan, withdraw challan and he will try to show that
Consider John is residing at a location A. Bank is residing at location B. The distance between A and B is five kilometers. To cover this distance, John is traveling by bike. So, for that, he needs to spend some amount on petrol and also the time. So, to move five kilometers, it would take some time. Thus, by using the first approach, John is spending both money and time for withdrawing 500 Rs.
Second Approach:
There’s a second approach where John visits the nearest ATM, and then he uses the bank debit card and enters the required credentials like PIN and other stuff, and then he withdraws the money.
Here, the proxy object is playing a significant role behind the scene. This proxy object is handling the request on behalf of us. So, by following the second approach, John is saving both time and money because nowadays ATM is present in every street. So, he could save both money and time to withdraw 500 Rs.
Let’s consider there are two objects. So, there’s a real object which is the placeholder for another object to control the access.
Proxy object will try to control the access to the real object.
Let’s suppose a client is trying to access the real object, proxy object will come into an act, and it will try to check whether the client has the permission to access the real object.
Here, the Proxy is the object that is being called by the client to access the real object behind the scene.
The client knows only the proxy object. The client isn’t aware of the real object. The proxy object will try to manage those internal things, and it will try to contact the real object and get the relevant information. The proxy object will serve the client.
For example, Let’s suppose if your client is trying to access the student data of a particular student. The client is accessing the “Student ID 1” data.
Here, the client doesn’t have access to the real object. The client has access only to the proxy object. The client knows that there are objects that will be served for the request. The client knows the real object won’t be created until the client requests the actual thing. Till that time this proxy would be shown to the client.
When the client sends a request then only that the Server would create real object. For example, “Student ID 1” is the actual request submitted during the runtime, and that actual data would be converted to the real object. That real object would be served to the client. So this is the procedure for the Proxy Design Pattern, Virtual proxy.
In software applications also, we are following Proxy Design Pattern for some use cases. So, you need , and you need to check whether that use case could fit in the Proxy Design Pattern or not.
The remote proxy provides a local representative for an object that resides in different address spaces.
For example, Let’s suppose if the actual data is present at various spaces. The client is unaware of those things. The client knows there would be some object that would be served when it triggers a request.
So here the proxy object would handle those things. The proxy object would get the object from the better address place for the particular request.
Protected proxy controls access to the sensitive master object. Let’s suppose if you’re studying in a college, you would have some lab sessions, and during those labs sessions, you can access the internet.
Let’s suppose if you’re trying to access social websites like Facebook, WhatsApp, etc. using college internet network. Then there will be some proxy server, which won’t allow you to access the Facebook server or WhatsApp server.
Here, this is acting as a Protective proxy. It is protecting the actual request based on the various criteria.
public interface Download { public void download(); }
public class RealDownload implements Download { private String targetFile; private String targetData; public RealDownload(String targetFile) { this.targetFile = targetFile; downloadFromInternet(); } private void downloadFromInternet() { this.targetData="This is a test data "; } @Override public void download() { System.out.println(this.targetData); } }
public class ProxyDownload implements Download { private RealDownload realDownload; private String targetFile; public ProxyDownload(String targetFile) { this.targetFile=targetFile; } @Override public void download() { if(realDownload==null) { realDownload=new RealDownload(targetFile); } realDownload.download(); } }
public class ProxyDemo { public static void main(String[] args) { Download download=new ProxyDownload("xyz.movie"); download.download(); System.out.println(""); download.download(); } }