Saturday, September 13, 2014

Adapter Design Pattern

Definition

Adapter Pattern is a structural design pattern that supports providing common interface to communicate with incompatible interfaces.

Intention
  1. Convert the interface of a class into another interface clients expect.
  2. Adapter lets classes work together, that could not otherwise because of incompatible interfaces.
Components

Target - This is the domain specific interface that the Client will use.
Adaptee - This is the existing interface that needs adapting.
Adapter - Converts Adaptee to the target interface.
Client - This is the class which uses Adapter to convert Adaptee to the target interface.

There are 2 flavors of this pattern

  1.      Object Adapter  - Uses aggregation to implement the pattern. 
  2.      Class Adapter - Uses inheritance to implement the pattern.(by multiple inheritance)

Object Adapter

This is the common way of using the pattern. This type can be implemented using aggregation with the Adapter.

 


In this example you will see that we aggregate ForeignUser as a property in Adapter and extend User class.
So that we can override User class methods which include the use of adaptee class operations and provide the client expected target output.

User(Target):

public class User {
    private String name = "John Spencer";
   
 public String    getName(){
     return this.name;
 }
}


Foreign User(Adaptee)

public class ForeignUser {

    private String pedgreeName = "Don Velarathna Mudiyanselage";
    private String firstName = "Kumar ";
    private String  middleName = "Nirosh ";
    private String  lastName = "Thennakoon ";
    public String getPedgreeName() {
        return pedgreeName;
    }
    public String getFirstName() {
        return firstName;
    }
    public String getMiddleName() {
        return middleName;
    }
    public String getLastName() {
        return lastName;
    }
   
   
}


Adapter

public class Adapter extends User {

    private ForeignUser foreignUser;
   
    public Adapter(ForeignUser foreignUser) {
     this.foreignUser = foreignUser;
    }

    @Override
    public String getName() {
       
        return foreignUser.getPedgreeName()+foreignUser.getFirstName()+foreignUser.getMiddleName()+foreignUser.getLastName();
    }

   

}


Client

public class Client {

    public static void main(String[] args) {

        User user = new User();
        System.out.println(user.getName()); // Output: John Spencer
        User user2 = new Adapter(new ForeignUser());
        System.out.println(user2.getName()); // Output: Don Velarathna
                                                // MudiyanselageKumar Nirosh
                                                // Thennakoon

    }


Class Adapter

This type of the pattern is implemented by multiple inheritance. As java does not support direct multiple inheritance via extending 2 super classes, we have to use implementing an interface.


 







Adapter

public interface Adapter {

    String getPedgreeName();

    String getFirstName();

    String getMiddleName();

    String getLastName();

}

Adaptee

    public class ForeignUserAdapter extends User implements Adapter {

    private String pedgreeName = "Don Velarathna Mudiyanselage ";
    private String firstName = "Kumar ";
    private String  middleName = "Nirosh ";
    private String  lastName = "Thennakoon ";
    @Override
    public String getPedgreeName() {
        return pedgreeName;
    }
    @Override
    public String getFirstName() {
        return firstName;
    }
    @Override
    public String getMiddleName() {
        return middleName;
    }
    @Override
    public String getLastName() {
        return lastName;
    }
    @Override
    public String getName() {
   
        return getPedgreeName()+getFirstName()+getMiddleName()+getLastName();
    }
   
}


Client

public class Client {

    public static void main(String[] args) {

        User user = new User();
        System.out.println(user.getName()); // Output: John Spencer
        User user2 = new ForeignUserAdapter();
        System.out.println(user2.getName()); // Output: Don Velarathna
                                                // Mudiyanselage Kumar Nirosh
                                                // Thennakoon

    }

}

No comments: