Adapter Design Pattern in PHP

1. Definition

The Adapter Design Pattern acts as a bridge between two incompatible interfaces. It allows classes with incompatible interfaces to work together by providing a middle layer that translates calls from one interface to another.

2. Problem Statement

Imagine you have a legacy system with classes that your new system needs to communicate with. However, the methods or ways they communicate are not in sync. Direct integration could be problematic, leading to tangled code and potential bugs.

3. Solution

The Adapter Pattern offers a solution by allowing you to create a new interface for the legacy system, one that matches what your new system expects. This adapter then translates calls from the new system into a form that the old system can understand.

4. Real-World Use Cases

1. Translating data from legacy databases to newer systems.

2. Making third-party libraries compatible with your existing system.

3. Adapting interfaces in payment gateways to provide a consistent API.

5. Implementation Steps

1. Identify the existing target interface.

2. Understand the interface you need to adapt to.

3. Create the adapter class that implements the target interface and composes the adaptee.

4. In the adapter, convert method calls from the target interface to the appropriate calls on the adaptee.

6. Implementation in PHP

<?php
// Step 1: Define the target interface
interface Target {
    public function request(): string;
}
// Step 2: Define the adaptee with a different method
class Adaptee {
    public function specificRequest(): string {
        return "Specific request from the Adaptee.";
    }
}
// Step 3: Create the Adapter
class Adapter implements Target {
    private $adaptee;
    public function __construct(Adaptee $adaptee) {
        $this->adaptee = $adaptee;
    }
    public function request(): string {
        // Convert the call to the specificRequest method of the Adaptee
        return $this->adaptee->specificRequest();
    }
}
// Client code
$adaptee = new Adaptee();
$adapter = new Adapter($adaptee);
echo $adapter->request(); // Outputs: Specific request from the Adaptee.
?>

Output:

Specific request from the Adaptee.

Explanation:

In the code, the Target interface defines a request method, which the client expects. The Adaptee class, on the other hand, has a method named specificRequest.

To make these two compatible, we use the Adapter class that implements the Target interface. Inside the Adapter, when the request method is called, it translates this call to the specificRequest method of the Adaptee. In essence, the Adapter class bridges the gap between the Target and the Adaptee.

7. When to use?

The Adapter Pattern is beneficial when:

1. You want to use an existing class in your system, but its interface doesn't match what you need.

2. You want to create a reusable class that cooperates with classes that have incompatible interfaces.

3. You need to integrate with systems or libraries where you can't change the source code, but you can introduce an adapter to communicate.


Comments