Adapter Design Pattern By: Klementinus Kennyvito Salim What is Adapter? • Adapter design pattern is basically similar to a hardware adapter , except now it is for a program class. • It converts the interface of a class into another interface that the client class can accept and work together with Adapter Diagram Geeksforgeeks.com Sample Code interface Cat { public void walk(); public void meow(); } class Persian implements Cat { public void walk() { System.out.println("Walking"); } public void meow() { System.out.println("Meow Meow"); } } interface Cat_Toy { // target interface // Just a toy not walking public void makeSound(); } class Calling_Cat implements Cat_toy { public void makeSound() { System.out.println("Squeak"); } } Sample Code class Adapter implements Cat_Toy { // You need to implement the interface your // client expects to use. Cat cat; public Adapter(Cat cat) { // reference to object we adapted this.cat = cat; } public void makeSound() { // translate the methods appropriately cat.meow(); } } class Main { public static void main(String args[]) { Persian persian = new Persian(); Cat_Toy toy = new Cat_Toy(); // Calling the adapter class to make the interface compatible Cat_Toy Adapter = new Adapter(persian); System.out.println("Cat_toy..."); toyDuck.makeSound(); System.out.println("Adapter..."); Adapter.makeSound(); } } The Advantages and Disadvantages of using Adapter Design Pattern Advantages • Achieve flexibility and reusability of classes and functions Disadvantages • Sometimes many adaptations are required among many adapters so that a certain type is reached Thank You