0% found this document useful (0 votes)
13 views3 pages

Proxy Design Pattern

Uploaded by

aodhora111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Proxy Design Pattern

Uploaded by

aodhora111
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Proxy Design Pattern

Simply, proxy means an object representing another object.


According to GoF, a Proxy Pattern "provides the control for accessing the original object".
So, we can perform many operations like hiding the information of original object, on demand
loading etc.
Proxy pattern is also known as Surrogate or Placeholder.
Advantage of Proxy Pattern
o It provides the protection to the original object from the outside world.

Usage of Proxy Pattern:


It is used:
o It can be used in Virtual Proxy scenario---Consider a situation where there is multiple
database call to extract huge size image. Since this is an expensive operation so here we
can use the proxy pattern which would create multiple proxies and point to the huge
size memory consuming object for further processing. The real object gets created only
when a client first requests/accesses the object and after that we can just refer to the
proxy to reuse the object. This avoids duplication of the object and hence saving
memory.
o It can be used in Protective Proxy scenario---It acts as an authorization layer to verify
that whether the actual user has access the appropriate content or not. For example, a
proxy server which provides restriction on internet access in office. Only the websites
and contents which are valid will be allowed and the remaining ones will be blocked.
o It can be used in Remote Proxy scenario---A remote proxy can be thought about the
stub in the RPC call. The remote proxy provides a local representation of the object
which is present in the different address location. Another example can be providing
interface for remote resources such as web service or REST resources.
o It can be used in Smart Proxy scenario---A smart proxy provides additional layer of
security by interposing specific actions when the object is accessed. For example, to
check whether the real object is locked or not before accessing it so that no other
objects can change it.
Example:
package proxyadapter;
/** * * @author User */
public interface Conncection {
void createConnection(String sender, String receiver);
}
public class Internet implements Conncection{
@Override
public void createConnection(String sender, String receiver) {
System.out.println("Connecting " + sender + " to " + receiver); }
}}
public class Bluetooth implements Conncection{
@Override
public void createConnection(String sender, String receiver) {
System.out.println("Connecing via bluetooth");
}}
public class ProxyInternet implements Conncection{
@Override
public void createConnection(String sender, String receiver) {
Internet i = new Internet();
if(receiver.equals("192.168.1.1")) {
System.out.println("Receiver unauthorized.");
return;
}
i.createConnection("192.168.0.1", "192.168.1.1");
}
}

public class Proxy {


public static void main(String[] args) {
Conncection i = new ProxyInternet();
i.createConnection("192.168.0.1", "192.168.1.1");
}
}

You might also like