In this section, we will explain the concept of proxy design pattern with real-time examples. in the definition world, the proxy is a
replacement for a real one. Our atm card is a proxy to access the bank account. the proxy design pattern is one of the structural design patterns. We use that as cash whenever it is needed. There are similar types of patterns available in java. it is used to improve performance. substitute acted as real. it is the authority to act for another. a proxy is an object that controls access to another object. proxy is to act on the behalf of another person. In our java example, we will be using a proxy instead of a real object. the proxy will communicate with real objects. proxy is a mediator between the client and the real object. the proxy can be used anywhere when a real object is needed. proxy controls access to real objects. In our example, a proxy will be a composition relationship with a real object. it increases performance. on-demand, we will be loading images.
replacement for a real one. Our atm card is a proxy to access the bank account. the proxy design pattern is one of the structural design patterns. We use that as cash whenever it is needed. There are similar types of patterns available in java. it is used to improve performance. substitute acted as real. it is the authority to act for another. a proxy is an object that controls access to another object. proxy is to act on the behalf of another person. In our java example, we will be using a proxy instead of a real object. the proxy will communicate with real objects. proxy is a mediator between the client and the real object. the proxy can be used anywhere when a real object is needed. proxy controls access to real objects. In our example, a proxy will be a composition relationship with a real object. it increases performance. on-demand, we will be loading images.client will interact
advantages of using proxy pattern:
1. expensive objects: instead of directly working on expensive objects. Firstly, we will deal with a proxy that has the same interface as an expensive object. it will take care of when to initiate the object. It works on demand. suppose you have a document with 1000 pages. Each page contains an image. instead of loading all the images at the application startup. we will load each image when that particular page is visited. It is known as lazy loading. it improves application performance.
2. authentication purpose.
3. database operations.
Java Proxy design pattern with real time examples :
public class ProxypatternJavaDemo {
public static void main(String[] args) {
System.out.println("Proxy Pattern Java Demo");
ProxyImage proxy=new ProxyImage("admin", "password");
proxy.queryRecords();
System.out.println("i want to query the same data somewhere else");
proxy.queryRecords();
}
}
interface Database {
void queryRecords();
}
class MySqlConnection implements Database { // Real Object
String userName;
String password;
public MySqlConnection(String userName, String password) {
System.out.println("Initialized...");
this.userName = userName;
this.password = password;
checkAuthentication();
}
@Override
public void queryRecords() {
System.out.println("Querying....");
}
private void checkAuthentication() {
System.out.println("Authentication successful...");
}
}
class ProxyImage implements Database { // Proxy Object
private MySqlConnection mySQLConnection;
private String userName;
private String password;
public ProxyImage(String userName, String password) {
this.userName = userName;
this.password = password;
}
@Override
public void queryRecords() {
if (mySQLConnection == null) {
mySQLConnection = new MySqlConnection(userName, password);
}
mySQLConnection.queryRecords();
}
}
output:
Proxy Pattern Java Demo
Initialized...
Authentication successful...
Querying....
Querying....
In the above example, MySqlConnection is a real object class. ProxyImage is a proxy object which controls the access of real object from the client. client can’t directly access real object ( MySqlConnection ) but through a mediator (ProxyImage ), it is possible. if you see the above example, when we create a proxy object class, directly SQL connection classes will not be loaded or initialized. by the following proxy design pattern, the database connection will be initialized only on demand which is known as lazy loading. we can save on expensive operations costs. if you see the output of this program, only a database connection established once for the multiple uses. Proxy design pattern will improve the performance of your application. hope you are liking my blog, please keep supporting me by sharing your valuable comments.
Leave a comment