Adapter

Definition


Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatibl

UML class diagram


Participants


  • Target
    • defines the domain-specific interface that Client uses.
  • Adapter
    • adapts the interface Adaptee to the Target interface.
  • Adaptee
    • defines an existing interface that needs adapting.
  • Client
    • collaborates with objects conforming to the Target interface.

Sample code in Java

package com.hong.adapter;

interface Target{
    
    public void reqeust();
}

class Adapter implements Target
{
    private Adaptee adaptee; 
    
    public Adapter()
    {
        adaptee =new Adaptee();
    }

    @Override
    public void reqeust() {
        // TODO Auto-generated method stub
        adaptee.request();
    }
}

class Adaptee
{
    public void request()
    {
        System.out.println("Acquires a real request from adaptee class");
    }
}

public class Adapter_structure {
    
    public static void main(String[] args)
    {
        Target target = new Adapter(); 
        target.reqeust();
    }

}