Proxy design pattern in java - Java @ Desk

Thursday, June 20, 2013

Proxy design pattern in java

Proxy, as the name suggest, does the work what the real one should do. Proxy is some thing that performs the task what the real java object must perform to do.Typically, one instance of the complex object and multiple proxy objects are created, all of which contain a reference to the single original complex object. Any operations performed on the proxies are forwarded to the original object. Once all instances of the proxy are out of scope, the complex object's memory may be deallocated.

Why to use proxy?
There can be many instances, where real object wants the task to be performed by the proxy object. In this case, proxy first checks if the real instance object is available or not. It not create the new instance but if the object is already available, do not create the new instance, proxy instance will be used to perform the operation.

Consider an exqample of salary calculation for an employee. We pass the employeeId and annualPackage and the method returns the monthly in hand amount salary for an employee. But this method involves lot of complex calculation.


Salary.java - This is the interface where the implementation would perform salary calculation

package com.designpatterns.proxy;

public interface Salary {

    public double calculateSalary();
    
}

SalaryCalculator.java - This class performs salary calculation for an employee
package com.designpatterns.proxy;

public class SalaryCalculator implements Salary {

    public int employeeId;

    public double annualPackage;
    
    public double salary;

    public SalaryCalculator(int employeeId, double annualPackage) {
        this.employeeId = employeeId;
        this.annualPackage = annualPackage;
    }

    @Override
    public double calculateSalary() {
        double salary = this.annualPackage / 12;
        if (annualPackage > 200000 && annualPackage < 500000) {
            salary = salary - (salary * 0.1);
        } else if (annualPackage > 50000) {
            salary = salary - (salary * 0.2);
        }
        this.setSalary(salary);
        return salary;
    }
    
    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

}

ProxySalaryCalculator.java - This is the proxy of the above class
package com.designpatterns.proxy;

public class ProxySalaryCalculator implements Salary {

    private SalaryCalculator salaryCalculator;

    public int employeeId;

    public double annualPackage;
    
    public double salary;
    
    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public ProxySalaryCalculator(int employeeId, double annualPackage) {
        this.employeeId = employeeId;
        this.annualPackage = annualPackage;
    }
    
    @Override
    public double calculateSalary() {

        if (salaryCalculator == null) {
            System.out.println("Instance created");
            salaryCalculator = new SalaryCalculator(this.employeeId, this.annualPackage);
            salaryCalculator.calculateSalary();
        }
        return salaryCalculator.getSalary();
    }
}

ProxyMain.java - Java class to test the implementation
package com.designpatterns.proxy;

public class ProxyMain {

    public static void main(String args[]) {
        int employeeId = 105;
        double annualPackage = 400000;
        Salary salary = new ProxySalaryCalculator(employeeId, annualPackage);
 // This will create new instance of Real object and perform complex processing
        System.out.println("Salary for employee number : " + employeeId + " is " + salary.calculateSalary());
 // This will use the proxy instance to calculate salary
        System.out.println("Salary for employee number : " + employeeId + " is " + salary.calculateSalary());
    }
}








No comments:

Post a Comment