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.
?>
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?