Explain the Adapter pattern

The Adapter Pattern is a structural design pattern that allows incompatible interfaces to work together. It acts as a bridge between two incompatible interfaces by converting the interface of a class into another interface that a client expects. This pattern is particularly useful when you want to integrate new components into existing systems without changing their interfaces or when you want to wrap external libraries or systems to allow them to be used more seamlessly within a project.

In essence, the Adapter Pattern allows classes to work together that couldn’t otherwise due to incompatible interfaces.

<?php // Target interface interface Target { public function request(); } // Adaptee class with a different interface class Adaptee { public function specificRequest() { return "Specific request."; } } // Adapter class that makes the adaptee compatible with the target interface class Adapter implements Target { private $adaptee; public function __construct(Adaptee $adaptee) { $this->adaptee = $adaptee; } public function request() { return $this->adaptee->specificRequest(); } } // Client code function clientCode(Target $target) { echo $target->request(); } // Usage $adaptee = new Adaptee(); $adapter = new Adapter($adaptee); clientCode($adapter); // Outputs: Specific request. ?>

Adapter pattern design patterns software engineering structural patterns