in design pattern php oop object oriented programming ~ read.

Adaptor Pattern in PHP

In this post we are going to talk about Adaptor Pattern in Object Oriented Programming.

What is Adaptor Pattern?

There are shit tons of definition out there if you just google it. So here I provide my really simple definition of what Adaptor Pattern is. Imagine there are two classes that are really similar to each other but there are some fields in class A that are not compatible with class B and you don't have the access into their API. What are you gonna do? It is why better to understand something given some situation.

Demo

Here we are going to write some code utilising Adaptor Pattern in PHP. First let's create a class called WorkerA.

<?php  
class WorkerA implements Worker {

    private $id;
    private $firstName;
    private $lastName;
    private $email;

    public function __construct($id, $firstName, $lastName, $email) {
       $this->id        = $id;
       $this->firstName = $firstName;
       $this->lastName  = $lastName;
       $this->email     = $email;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getFirstName()
    {
        return $this->firstName;
    }

    public function getLastName()
    {
        return $this->lastName;
    }

    public function getEmail()
    {
        return $this->email;
    }
}

(WorkerA)

Next obvious we need to create Worker interface as well.

<?php

interface Worker  
{
    public function getId();
    public function getFirstName();
    public function getLastName();
    public function getEmail();
}

Next let's say we have another Worker class called WorkerB like the following.

<?php

class WorkerB {

    private $workerId;
    private $givenName;
    private $surName;
    private $emailAddress;

    public function __construct($workerId, $givenName, $surName, $emailAddress) {
       $this->workerId      = $workerId;
       $this->givenName     = $givenName;
       $this->surName       = $surName;
       $this->emailAddress  = $emailAddress;
    }

    public function getWorkerId()
    {
        return $this->workerId;
    }

    public function getGivenName()
    {
        return $this->givenName;
    }

    public function getSurName()
    {
        return $this->surName;
    }

    public function getEmailAddress()
    {
        return $this->emailAddress;
    }
}

(WorkerB)

Next let's say we want to convert WorkerB class into WorkerA class. How are we supposed to do that?

Let's introduce a new class named WorkerAdaptor like the following.

<?php

class WorkerAdaptor implements Worker {

    private $instance;

    public function __construct($instance) {
       $this->instanceof = $instance;
    }

    public function getId()
    {
        return $instance->workerId;
    }

    public function getFirstName()
    {
        return $instance->givenName;
    }

    public function getLastName()
    {
        return $instance->surName;
    }

    public function getEmail()
    {
        return $instance->emailAddress;
    }
}

(WorkerAdaptor)

So now we can use WorkerAdaptor class to convert WorkerB to WorkerA.

End

Hope this crash course is useful to you guys. And if you have any opinions or ideas please leave it below. Thanks and happy coding!

comments powered by Disqus