The Adapter pattern converts the interface of a class into another interface that clients expect. It allows classes to work together that couldn't otherwise because of incompatible interfaces.
The example implements the Adapter pattern:
- A
RectangleLegacyclass with an incompatible interface - A
RectangleShapeAdapterthat adapts the legacy rectangle to a new interface - The adapter translates calls from the client to the adaptee (legacy object)
$rectangle = new RectangleShapeAdapter(new RectangleLegacy());
$result = $rectangle->display($x1=10, $y1=10, $x2=20, $y2=20);
$this->assertTrue($result);- When you want to use an existing class, but its interface doesn't match what you need
- When you want to create a reusable class that cooperates with classes that don't necessarily have compatible interfaces
- When you need to use several existing subclasses but it's impractical to adapt their interface by subclassing each one
- When you need to make components work together that weren't designed to work together
- When you need to integrate components from different libraries or frameworks
