Adapter design pattern
The adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface. It is often used to make existing classes work with others without modifying their source code.
An example is an adapter that converts the interface of a Document Object Model of an XML document into a tree structure that can be displayed.
An adapter allows two incompatible interfaces to work together. This is the real-world definition for an adapter. Interfaces may be incompatible, but the inner functionality should suit the need. The adapter design pattern allows otherwise incompatible classes to work together by converting the interface of one class into an interface expected by the clients.
The adapter design pattern solves problems like:
- How can a class be reused that does not have an interface that a client requires?
- How can classes that have incompatible interfaces work together?
- How can an alternative interface be provided for a class?
- Often an (already existing) class can’t be reused only because its interface doesn’t conform to the interface clients require.
The adapter design pattern describes how to solve such problems:
- Define a separate adapter class that converts the (incompatible) interface of a class (adaptee) into another interface (target) clients require.
- Work through an adapter to work with (reuse) classes that do not have the required interface.
- The key idea in this pattern is to work through a separate adapter that adapts the interface of an (already existing) class without changing it.
Clients don’t know whether they work with a target class directly or through an adapter with a class that does not have the target interface.
interface BookInterface {
public function open();
public function turnPage();
}
class Book implements BookInterface {
public function open() {
var_dump("Open the paper book page.");
}
}
interface eReaderInterface {
public function turnOn();
public function pressNextButton();
}
class Kindle implements eReaderInterface {
public function turnOn() {
var_dump("Turn on the kindle");
}
public function pressButton() {
var_dump("press the next button");
}
public function turnPage() {
var_dump("Turn on the paper book page.");
}
}
class KindleAdapter implements BookInterface {
private $kindle;
public function __construct(Kindle $kindle) {
$this->kindle = $kindle;
}
public function open() {
return $this->kindle->turnOn();
}
public function turnPage() {
return $this->kindle->pressNextButton();
}
}
//A person can a read book or kindle
class Person {
public function read(BookInterface $book) {
$book->read();
$book->turnPage();
}
}
(new Person)->read(new Book());
(new Person)->read(new kindleAdapter(new Kindle));
More details can be found on my Github profile here.