The Adapter Pattern is a structural design pattern that allows objects with incompatible interfaces to collaborate. 
It acts as a bridge between two incompatible interfaces, converting the interface of one class into another interface that clients expect. 
The Adapter Pattern is particularly useful when you have existing classes that you want to reuse in a new system, 
but their interfaces do not match the requirements of the new system.

// Java implementation of Adapter pattern

interface Bird
{
   
// birds implement Bird interface that allows
    // them to fly and make sounds adaptee interface
   
public void fly();
   
public void makeSound();
}

class Sparrow implements Bird
{
   
// a concrete implementation of bird
   
public void fly()
    {
       
System.out.println("Flying");
    }
   
public void makeSound()
    {
       
System.out.println("Chirp Chirp");
    }
}

interface ToyDuck
{
   
// target interface
    // toyducks dont fly they just make
    // squeaking sound
   
public void squeak();
}

class PlasticToyDuck implements ToyDuck
{
   
public void squeak()
    {
       
System.out.println("Squeak");
    }
}

class BirdAdapter implements ToyDuck
{
   
// You need to implement the interface your
    // client expects to use.
   
Bird bird;
   
public BirdAdapter(Bird bird)
    {
       
// we need reference to the object we
        // are adapting
       
this.bird = bird;
    }

   
public void squeak()
    {
       
// translate the methods appropriately
       
bird.makeSound();
    }
}

class Main
{
   
public static void main(String args[])
    {
       
Sparrow sparrow = new Sparrow();
       
ToyDuck toyDuck = new PlasticToyDuck();

       
// Wrap a bird in a birdAdapter so that it
        // behaves like toy duck
       
ToyDuck birdAdapter = new BirdAdapter(sparrow);

       
System.out.println("Sparrow...");
       
sparrow.fly();
       
sparrow.makeSound();

       
System.out.println("ToyDuck...");
       
toyDuck.squeak();

       
// toy duck behaving like a bird
       
System.out.println("BirdAdapter...");
       
birdAdapter.squeak();
        // birdAdapter.fly(); // error because ToyDuck interface doesnt have fly method
       
    }
}

 

 

Sparrow...

Flying

Chirp Chirp

 

 

ToyDuck...

Squeak

 

 

BirdAdapter...

Chirp Chirp

 

 

 

Target: This is the interface that the client code expects to interact with.

Adaptee: This is the existing class or interface that needs to be adapted to work with the client code.

Adapter: This is the class that bridges the gap between the Target interface and the Adaptee interface.

It implements the Target interface and contains an instance of the Adaptee,

delegating the client's requests to the Adaptee in a format it can understand.

 

 

 

interface Target {
   
void request();
}

// Adaptee class with incompatible interface
class Adaptee {
   
void specificRequest() {
       
System.out.println("Adaptee's specific request");
    }
}

// Adapter class implementing the Target interface
class Adapter implements Target {
   
private Adaptee adaptee;

   
Adapter(Adaptee adaptee) {
       
this.adaptee = adaptee;
    }

   
@Override
    public void
request() {
       
adaptee.specificRequest(); // Delegating the request to the Adaptee
   
}
}

// Client code
public class Adapter2 {
   
public static void main(String[] args) {
       
Adaptee adaptee = new Adaptee();
       
Target adapter = new Adapter(adaptee);
       
adapter.request(); // Client code interacts with the Adapter, which delegates the request to the Adaptee
   
}
}

 

 

Adaptee's specific request

 

Insurance