Adapter design pattern in java - Java @ Desk

Thursday, June 13, 2013

Adapter design pattern in java

The Adapter pattern is used so that two unrelated interfaces can work together. The joining between them is called an Adapter. This is something like we convert interface of one class into interface expected by the client. We do that using an Adapter.

There is a SalaryCalculator.java class file which takes employeeId and annualPackage fields as input and returns the monthly inhand for that employee.

When a class with incompatible method needs to be used with another class you can use inheritance to create an adapter class. The adapter class which is inherited will have new compatible methods.

SalaryCalculator.java(Adaptee)
package com.designpatterns.adapter;

public class SalaryCalculator {

    public int employeeId;

    public double annualPackage;

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

    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);
        }
        return salary;
    }
}

SalaryCalculatorMain.java

package com.designpatterns.adapter;

public class SalaryCalculatorMain {

    public static void main(String args[]) {
        
        SalaryCalculator salaryCalculator = new SalaryCalculator(105, 900000);
        System.out.println(salaryCalculator.calculateSalary());
    }
}

Now the client has changes its requirement in a way that, for each employee whose employee type is IT then in that case a variable amount also need to be deducted alongwith the TAX that is already being deducted in SalaryCalculator.java But the thing is its not possible now to change the implementation of SalaryCalculator.java for many reasons like its a third party library or some other classes also using this class. So there arise a need to introduce an interface in between that will deduct the variable amount from the salary we get from calculateSalary() method of SalaryCalculator.java as shown below.

VariableDeductor.java(Target)

package com.designpatterns.adapter;

public interface VariableDeductor {
    
    public double calculateSalary(int employeeId , double annualPackage, String employeeType);
}

VariableDeductorImpl.java(Adapter) - This method will make a call to calculateSalary() method of SalaryCalculator.java and checks the employee type and apply variable deduction on the salary if the employee type is IT.

package com.designpatterns.adapter;

package com.designpatterns.adapter;

public class VariableDeductorImpl implements VariableDeductor {

    @Override
    public double calculateSalary(int employeeId, double annualPackage, String employeeType) {

        SalaryCalculator salaryCalculator = new SalaryCalculator(employeeId, annualPackage);
        double salary = salaryCalculator.calculateSalary();

        if (employeeType.equals("Admin") || employeeType.equals("GeneralStaff") || employeeType.equals("HR")) {
            return salary;
        } else if (employeeType.equals("IT")) {
            if (annualPackage > 200000 && annualPackage < 500000) {
                salary = salary - 4000;
            } else if (annualPackage > 50000) {
                salary = salary - 8000;
            }
            return salary;
        }
        return salary;
    }
}

Modified client file

SalaryCalculatorMain.java(Modified)

package com.designpatterns.adapter;

public class SalaryCalculatorMain {

    public static void main(String args[]) {
        
        VariableDeductor variableDeductor = new VariableDeductorImpl();
        int employeeId = 105;
        double annualPackage = 400000;
        String employeeType = "Admin";
        
        double salary = variableDeductor.calculateSalary(employeeId, annualPackage, employeeType);
        
        System.out.println("Salary for employee " + employeeId + " is : " + Math.round(salary));
        
        employeeId = 110;
        annualPackage = 800000;
        employeeType = "IT";
        
        salary = variableDeductor.calculateSalary(employeeId, annualPackage, employeeType);
        
        System.out.println("Salary for employee " + employeeId + " is : " + Math.round(salary));
                
    }
}








No comments:

Post a Comment