Skip to content

Latest commit

 

History

History
65 lines (51 loc) · 1.13 KB

adapter.md

File metadata and controls

65 lines (51 loc) · 1.13 KB

Adapter Design Pattern

The Adapter Pattern allows incompatible interfaces to work together by translating one interface into another.

  • Adaptee: The class with an incompatible interface that needs to be adapted.
  • Adapter: The class that implements the client's expected interface and translates requests to the adaptee.

How to apply

// Adaptee

class AudioBook
{
    public function __construct(private string $name) {}

    public function listen(): void {
        echo "Listening: " . $this->name;
    }
}
// The interface expected by the client

interface Book
{
    public function read(): void;
}
// Adapter

class BookAdapter implements Book
{
    public function __construct(private AudioBook $audioBook) {}

    public function read(): void
    {
        // Speech to text API code
        $this->audioBook->listen();
    }
}
// Client

function bookClient(Book $book): void
{
    $book->read();
}

Usage:

$audioBook = new AudioBook("The Hobbit");
$bookAdapter = new BookAdapter($audioBook);

bookClient($bookAdapter);

Output:

Listening: The Hobbit