Design Pattern: Adapter Pattern

Are you a developer ? Do you develop softwares ? If the answers of both is yes, most probably you know the term “design pattern”. Yeah, it is something then let one use an already proven solution to a common problem in a structured way so that it would be helpful for other developers to collaborate with the code, even if the project is big enough. There are many design patterns for different problems. Adapter is useful one them. Let’s talk about it in this article.

In general, what is adapter ? It is something that translate an object to make it suitable to fit with another object. You have a memory card, but your laptop has no port to insert it. In that case, you will use a card reader. You insert that memory card in that card reader and then the card reader will be plugged with the laptop with an USB. Here card reader is playing role like an adapter to fit/link the memory card with the laptop. So simple, not that ? Same thing/rule applies for the adapter design pattern.

Consider, we have a car class, which has two methods, start() and run()

class Car {

public function start() {
var_dump( 'The car is starting' );
echo '<br>';
}

public function run() {
var_dump('The car is running' );
echo '<br>';
}
}

Consider another class Man which can operate the Car object

class Man {

    public function operate(Car $car)
    {
        $car->start();
        $car->run();
    }
}

You can initiate this like below

(new Man()) -> operate( new Car() );

No, issues with it

string(19) “The car is starting”
string(18) “The car is running”

Now, let’s consider a class Plane having start() and fly() methods

class Plane {
public function start() {
var_dump( 'The plane is starting' );
echo '<br>';
}

public function fly() {
var_dump('The plane is flying' );
echo '<br>';
}
}

If we want to pass this class’s object to man class like below

(new Man()) -> operate( new Plane() );

It will show error because the operate() method runs start() and run() method which a class has, but the plane class doesn’t have the run() method, insteas it has fly() method.

So, what to do ? We have to have an adapter that makes the plane class to suitable to pass to Man’s operate() method so that it will run smoothly.

Let’s do that.

First, let’s get back to Car class, and extract the interface from it.

interface CarInterface {
public function start();
public function run();
}

Implement this interface to Car class

class Car implements CarInterface {

public function start() {
var_dump( 'The car is starting' );
echo '<br>';
}

public function run() {
var_dump('The car is running' );
echo '<br>';
}
}

Now let’s modify the Man class a bit. Pass CarInterface to operate() method instead of Car class itself

class Man {

public function operate(CarInterface $car)
{
$car->start();
$car->run();
}
}

Now, let’s make an adapter class named PlaneAdapter and implement CarInterface on it.

class PlaneAdapter implements CarInterface {

private $plane;

public function __construct(Plane $plane)
{
$this->plane = $plane;
}

public function start()
{
$this->plane->start();
}

public function run()
{
$this->plane->fly();
}
}

Look at the construct method of it. We have passed Plane class instance there. Then we have defined start() method which actually runs the start() method of plane, and run() method which actually runs the fly() method of it.

Now, we can pass the Plan instance wrapping it with PlanceAdapter class to make it suitable for the operate() method of Man instance.

(new Man()) -> operate( new PlaneAdapter( new Plane() ) );

Perfect !

string(21) “The plane is starting”
string(19) “The plane is flying”

This is what actually adapter class does ! 🙂

Happy coding.

Leave a Reply

Your email address will not be published. Required fields are marked *

four × three =