avatar

目录
Design Pattern: Adapter

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.

image-20200325152258772

  • Employee interface

    java
    1
    2
    3
    4
    5
    6
    7
    8
    package adapter;

    public interface Employee {
    public String getId();
    public String getFirstName();
    public String getLastName();
    public String getEmail();
    }
  • EmployeeDB that implements 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
    package 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

    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
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    package 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;
    }

    @Override
    public String toString() {
    return "adapter.EmployeeLdap{" +
    "cn='" + cn + '\'' +
    ", surName='" + surName + '\'' +
    ", giveName='" + giveName + '\'' +
    ", mail='" + mail + '\'' +
    '}';
    }
    }
  • EmployeeAdapterLdap implements Employee and bundle EmployeeLdap inside

    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
    package adapter;

    public class EmployeeAdapterLdap implements Employee {
    private EmployeeLdap instance;
    public EmployeeAdapterLdap(EmployeeLdap instance){
    this.instance = instance;
    }

    @Override
    public String getId() {
    return instance.getCn();
    }

    @Override
    public String getFirstName() {
    return instance.getSurName();
    }

    @Override
    public String getLastName() {
    return instance.getGiveName();
    }

    @Override
    public String getEmail() {
    return instance.getMail();
    }

    public String toSring(){
    return "ID:" + instance.getCn();
    }

    }
  • EmployeeClient that has List of Employee.

    java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    package 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;
    }

    }
文章作者: Aries Kou
文章链接: http://yoursite.com/posts/Design-Patterns/1628820726.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Aries' Blog
打赏
  • 微信
    微信
  • 支付寶
    支付寶

评论