0% found this document useful (0 votes)
98 views

Adapter Design Pattern: By: Klementinus Kennyvito Salim

The adapter design pattern converts the interface of an existing class into a compatible interface that clients expect. It acts as a bridge between two incompatible interfaces. An adapter allows classes to work together that otherwise could not due to incompatible interfaces. The code sample shows an adapter class that implements a Cat_Toy interface by adapting a Persian cat class that implements a Cat interface. The adapter's makeSound method calls the cat's meow method to translate the interface appropriately. Adapters provide flexibility and reusability by allowing incompatible classes to work together. However, achieving a certain interface may require multiple nested adapters.

Uploaded by

Sob Pob
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Adapter Design Pattern: By: Klementinus Kennyvito Salim

The adapter design pattern converts the interface of an existing class into a compatible interface that clients expect. It acts as a bridge between two incompatible interfaces. An adapter allows classes to work together that otherwise could not due to incompatible interfaces. The code sample shows an adapter class that implements a Cat_Toy interface by adapting a Persian cat class that implements a Cat interface. The adapter's makeSound method calls the cat's meow method to translate the interface appropriately. Adapters provide flexibility and reusability by allowing incompatible classes to work together. However, achieving a certain interface may require multiple nested adapters.

Uploaded by

Sob Pob
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

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
{ 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);
}

public void makeSound() System.out.println("Cat_toy...");


{ toyDuck.makeSound();
// translate the methods
appropriately System.out.println("Adapter...");
cat.meow(); Adapter.makeSound();
} }
} }
The Advantages and Disadvantages of using
Adapter Design Pattern

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

You might also like