Adapter Design Pattern: By: Klementinus Kennyvito Salim
Adapter Design Pattern: By: Klementinus Kennyvito Salim
Geeksforgeeks.com
Sample Code
interface Cat
{ interface Cat_Toy
{
public void walk(); // target interface
public void meow(); // Just a toy not walking
} public void makeSound();
}
class Persian implements Cat
{ class Calling_Cat implements Cat_toy
{
public void walk() public void makeSound()
{ {
System.out.println("Walking"); System.out.println("Squeak");
} }
public void meow() }
{
System.out.println("Meow
Meow");
}
}
Sample Code
class Adapter implements Cat_Toy class Main
{ {
// You need to implement the public static void main(String args[])
interface your {
// client expects to use. Persian persian = new Persian();
Cat cat; Cat_Toy toy = new Cat_Toy();
public Adapter(Cat cat) // Calling the adapter class to make the interface
{ compatible
// reference to object we adapted
this.cat = cat; Cat_Toy Adapter = new Adapter(persian);
}
Advantages Disadvantages
• Achieve flexibility and reusability of classes • Sometimes many adaptations are required
and functions among many adapters so that a certain type is
reached
Thank You