What is Adapter
Adapter is a structural design pattern that allows objects with incompatible interface to collaborate. The adapter acts as a wapper to adapt the new interface.
Example
EmployeeLdap is lagacy code without implementing Employee Interface. It representing employee entity that from Ldap. EmployeeDB is new code that implemented Employee interface.Now that we want have a Employee list that include both employee from both DB and ldap. In this case, we use adapter class to bundle this lagacy class - EmployeeLdap.
Employee interface
java1
2
3
4
5
6
7
8package adapter;
public interface Employee {
public String getId();
public String getFirstName();
public String getLastName();
public String getEmail();
}EmployeeDB that implements Employee
java1
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
31package adapter;
public class EmployeeDB implements Employee {
private String id;
private String firstName;
private String lastName;
private String email;
public EmployeeDB(String id, String firstName, String lastName, String email){
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}
public String getId() {
return null;
}
public String getFirstName() {
return null;
}
public String getLastName() {
return null;
}
public String getEmail() {
return null;
}
}EmployeeLdap doesn’t implement Employee
java1
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
47
48
49
50
51
52
53
54
55
56
57package adapter;
public class EmployeeLdap {
private String cn;
private String surName;
private String giveName;
private String mail;
public EmployeeLdap(String cn, String surName, String giveName, String mail) {
this.cn = cn;
this.surName = surName;
this.giveName = giveName;
this.mail = mail;
}
public String getCn() {
return cn;
}
public void setCn(String cn) {
this.cn = cn;
}
public String getSurName() {
return surName;
}
public void setSurName(String surName) {
this.surName = surName;
}
public String getGiveName() {
return giveName;
}
public void setGiveName(String giveName) {
this.giveName = giveName;
}
public String getMail() {
return mail;
}
public void setMail(String mail) {
this.mail = mail;
}
public String toString() {
return "adapter.EmployeeLdap{" +
"cn='" + cn + '\'' +
", surName='" + surName + '\'' +
", giveName='" + giveName + '\'' +
", mail='" + mail + '\'' +
'}';
}
}EmployeeAdapterLdap implements Employee and bundle EmployeeLdap inside
java1
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
33package adapter;
public class EmployeeAdapterLdap implements Employee {
private EmployeeLdap instance;
public EmployeeAdapterLdap(EmployeeLdap instance){
this.instance = instance;
}
public String getId() {
return instance.getCn();
}
public String getFirstName() {
return instance.getSurName();
}
public String getLastName() {
return instance.getGiveName();
}
public String getEmail() {
return instance.getMail();
}
public String toSring(){
return "ID:" + instance.getCn();
}
}EmployeeClient that has List of Employee.
java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21package adapter;
import java.util.ArrayList;
import java.util.List;
public class EmployeeClient {
public List<Employee> getEmployeeList(){
List<Employee> employees = new ArrayList<Employee>();
//add employeeDB into list
Employee employeeFromDB = new EmployeeDB("1234","Jone","Wick","john@wick.com");
employees.add(employeeFromDB);
//add employeeLdap into list
EmployeeLdap employeeLdap = new EmployeeLdap("chewie","Solo","Han","hand@solo.com");
employees.add(new EmployeeAdapterLdap(employeeLdap));
return employees;
}
}








