eaglewap Data Scientist | Software Engineer | Google Code U Alumni | Former WSO2 Software Engineering Intern | "Google Code U"Alumni | Former WSO2 Software Engineering Intern. No comments

Adapter Design Pattern

  • AKA wrapper
  • Use when an (external) component is not compatible with the current system
  • Convert the interface of a class into another interface that clients expect
  • –Wrap an existing class with a new interface
  • Lets classes work together, that could not otherwise because of incompatible interfaces
Purpose
Permits classes with disparate interfaces to work together by creating a common object by which they may communicate and interact.

Use When ??

  •  A class to be used doesn’t meet interface requirements.
  • n Complex conditions tie object behavior to its state.
  • n Transitions between states need to be explicit.

Class diagram for Adapter Design Pattern

Adapter

Match interfaces of different classes

Example Java Code for Adapter Design Pattern 😊

There is a problem in two different interfaces. Where HR system proceeds the Employee details in a String array and Other Third party Billing system wants the Details in a Array list to proceed. This is the scenario where Adapter is needed in order to change the Array list employees to Employee class array list . Adapter changes it implementing the target interface.

ITarget.java 

1
2
3
public interface ITarget {
    public void ProcessCompanyInfo(String[][] employeeInfo);
}
EmplyeeAdaptor.java 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.ArrayList;

/**
 *
 * @author -
 */
public class EmplyeeAdaptor implements ITarget {

    private ThirdpartyBillingSystem ThirdpartyBillingSystem;

    public EmplyeeAdaptor() {
        ThirdpartyBillingSystem = new ThirdpartyBillingSystem();
    }

    /*
    * This Adapter method converts String Array employee information to
    * ArrayList of Employees
    */
    @Override
    public void ProcessCompanyInfo(String[][] employeeInfo) {
        String empId = null;
        String name = null;
        String designation = null;
        ArrayList<Employee> employeeList = new ArrayList<Employee>();
        for (int i = 0; i < employeeInfo.length; i++) {

            for (int j = 0; j < employeeInfo[i].length; j++) {
                if (j == 0) {
                    empId = employeeInfo[i][j];
                } else if (j == 1) {
                    name = employeeInfo[i][j];
                } else {
                    designation = employeeInfo[i][j];
                }
            }
            employeeList.add(new Employee(name, Integer.parseInt(empId), designation));

        }

        System.out.println("Adapter converted Array of Employee to ArrayList of Employee : \n" + employeeList + "\n"
                + "then call the processSalary method inherited from the ThirdPartyBillingSystem for processing the employee salary");
        ThirdpartyBillingSystem.processSalary(employeeList);

    }

}
Employee.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
public class Employee {

    private String name;
    private int empId;
    private String designition;

    public Employee(String name, int empId, String designition) {
        this.name = name;
        this.empId = empId;
        this.designition = designition;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getEmpId() {
        return empId;
    }

    public void setEmpId(int empId) {
        this.empId = empId;
    }

    public String getDesignition() {
        return designition;
    }

    public void setDesignition(String designition) {
        this.designition = designition;
    }

    @Override
    public String toString() {
        return "Employee [empId=" + empId + ", name=" + name + ", designation=" + designition + "]";
    }

}
ThirdpartyBillingSystem.java 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public class ThirdpartyBillingSystem {

    //Its given by third party 
    //It can only process Array List of Employees
    //But we have Employee String Array
    public void processSalary(ArrayList<Employee> emplyeelist) {
        for (Employee employee : emplyeelist) {
            System.out.println(employee.toString() + ":");
            if (employee.getDesignition().equalsIgnoreCase("Team Leader")) {
                System.out.println("70000Rs Salary credited to " + employee.getName() + " Account\n");
            } else if (employee.getDesignition().equalsIgnoreCase("Developer")) {
                System.out.println("40000Rs Salary credited to " + employee.getName() + " Account\n");
            } else if (employee.getDesignition().equalsIgnoreCase("Tester")) {
                System.out.println("30000Rs Salary credited to " + employee.getName() + " Account\n");
            }

        }

    }
}
HRsystem.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/**
 *
 * @author -
 */
public class HRsystem {

    public static String[][] getEmployees() {
        String[][] employees = new String[4][];

        employees[0] = new String[]{"100", "Kugan", "Team Leader"};
        employees[1] = new String[]{"101", "Anusan", "Developer"};
        employees[2] = new String[]{"102", "Raju", "Developer"};
        employees[3] = new String[]{"103", "Ravid", "Tester"};

        return employees;
    }

    public static void main(String args[]) {

        ITarget target = new EmplyeeAdaptor();
        System.out.println("HR system passes employee string array to Adapter\n");
        target.ProcessCompanyInfo(getEmployees());

    }

}

Comments (0)

Post a Comment

Cancel