Design pattern in programming

Design Pattern: Adapter Pattern

Posted by
Spread the love

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.

Understanding the Adapter Design Pattern

In essence, an adapter translates an object to make it compatible with another object. Think of it like having a memory card without a compatible port on your laptop. In such a situation, you’d use a card reader as an adapter, bridging the gap between the memory card and the laptop through a USB connection. The Adapter Design Pattern operates on a similar principle.

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 ! 🙂

There are other patterns as well which are used in other different scenarios which you can learn about from here. Incorporating those ones like the one we discussed in this article enhances your coding efficiency and understanding. Explore other patterns to elevate your coding skills. Learning them and implementing them in everyday coding will improve your understanding and efficiency in it. Happy coding.

About The Author

Leave a Reply

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