How to implement the Adapter design pattern in PHP?

by lindsey.homenick , in category: PHP General , a year ago

How to implement the Adapter design pattern in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by arnoldo.moen , a year ago

@lindsey.homenick 

The Adapter design pattern is a structural pattern that allows incompatible interfaces to work together. In PHP, you can implement the Adapter pattern as follows:

  1. Define the Target interface that the client expects to work with:
1
2
3
interface Target {
    public function request();
}


  1. Create the Adaptee interface that the adapter will be working with:
1
2
3
interface Adaptee {
    public function specificRequest();
}


  1. Implement the Adaptee interface:
1
2
3
4
5
class ConcreteAdaptee implements Adaptee {
    public function specificRequest() {
        return "Adaptee specific request.";
    }
}


  1. Implement the Adapter class that implements the Target interface and uses the Adaptee:
1
2
3
4
5
6
7
8
9
class Adapter implements Target {
    private $adaptee;
    public function __construct(Adaptee $adaptee) {
        $this->adaptee = $adaptee;
    }
    public function request() {
        return $this->adaptee->specificRequest();
    }
}


  1. Use the Adapter class to make the Adaptee work with the Target interface:
1
2
3
$adaptee = new ConcreteAdaptee();
$adapter = new Adapter($adaptee);
echo $adapter->request(); // Output: Adaptee specific request.


In this example, the Target interface represents the interface that the client expects to work with. The Adaptee interface represents the interface that is incompatible with the Target interface. The ConcreteAdaptee class implements the Adaptee interface, and the Adapter class implements the Target interface and uses the Adaptee interface to make it work with the Target interface.

Member

by daisha , 10 months ago

@lindsey.homenick 

The Adapter design pattern is a structural pattern that allows the interface of an existing class to be used as another interface. This pattern is used when we have two incompatible interfaces, and we want to make them work together without modifying either interface.


To implement the Adapter design pattern in PHP, you can follow these steps:

  1. Define the target interface: This is the interface that the client expects to use. It is the interface that the adapter will implement.
1
2
3
4
interface TargetInterface
{
    public function request(): string;
}


  1. Define the Adaptee interface: This is the interface that needs to be adapted to the target interface. It is the interface that the adapter will wrap.
1
2
3
4
interface AdapteeInterface
{
    public function specificRequest(): string;
}


  1. Implement the Adaptee interface in the Adaptee class: This is the class that implements the Adaptee interface.
1
2
3
4
5
6
7
class Adaptee implements AdapteeInterface
{
    public function specificRequest(): string
    {
        return "Adaptee-specific request.";
    }
}


  1. Create the Adapter class: This is the class that implements the TargetInterface and wraps the Adaptee.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class Adapter implements TargetInterface
{
    private $adaptee;

    public function __construct(AdapteeInterface $adaptee)
    {
        $this->adaptee = $adaptee;
    }

    public function request(): string
    {
        return "Adapter: " . $this->adaptee->specificRequest();
    }
}


  1. Use the Adapter: In the client code, create an instance of the Adaptee and pass it to the Adapter.
1
2
3
$adaptee = new Adaptee();
$adapter = new Adapter($adaptee);
echo $adapter->request();


The output should be: "Adapter: Adaptee-specific request."


By following these steps, you can implement the Adapter design pattern in PHP.