1. The problem

Many many times, we have to face stuff like this: you want to implement a Class/Feature/Thirst party libs to your existing system, but it’s not suitable. The point is, you can’t change the object that you are trying to implement, that’s too risky, maybe. For example, we have a full-text search system now using the searching method from the default method of the database, the better searching solution is using an object of Elasticsearch method, but the object/libs implement Elasticsearch is not suitable with the existing searching one.


That’s the time we should think about the adapter pattern, we need a special object that converts the interface of one object so the other object could understand it.

2. The solution

Of course, the one thing that couldn’t be missed is the Adapter, the object that we want to implement to gonna be inject into the Adapter, then the system (Client) could use it.

Let see what we have to do in the case I mentioned in section one:

3. The rule

Just make sure that your adapter follows the client interface, so can really work !

The client also should use adapter via client interface, this will let you easily change the adapter without affecting the client

4. The Implementation

Let see how we implement this pattern in PHP. Ok at the very first time our program looks like this

<?php

class Client
{
    protected $searchSystem;

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

    public function excute($keyword)
    {
        return $this->searchSystem->search($keyword);
    }
}

class SQLDatabaseSystem
{
   	public function search($keyword)
    {
        return "Search '". $keyword. "'. Resulted by using database query!";
    }
}

$client = new Client(new SQLDatabaseSystem());
echo $client->excute('mina');

Now we have to use the Elasticsearch System like this

class ElasticSearchSystem
{
    public function find($keyword)
    {
        return "Find '". $keyword. "'. Resulted by find in Elasticsearch!";
    }
}

The ElasticsearhcSystem using find() method, you can’t inject it into Client the call search() method as the example above:

public function excute($keyword)
{
  return $this->searchSystem->search($keyword);
}

So what should we do? It’s time to build an adapter:

class Adapter 
{
    protected $adaptee;

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

    public function search($keyword)
    {
        return $this->adaptee->find($keyword);
    }
}

$elastic = new Adapter(new ElasticSearchSystem());

$client = new Client($elastic);
echo $client->excute('mina');

Don’t forget to use an interface, this ensures your adapter doesn’t do the dumb thing

interface SearchingInterface
{
    public function search($keyword);
}

Finally, our code looks like this:

<?php

class Client
{
    protected $searchSystem;

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

    public function excute($keyword)
    {
        return $this->searchSystem->search($keyword);
    }
}

interface SearchingInterface
{
    public function search($keyword);
}

class SQLDatabaseSystem implements SearchingInterface
{
    public function search($keyword)
    {
        return "Search '". $keyword. "'. Resulted by search by database query!";
    }
}

class ElasticSearchSystem
{
    public function find($keyword)
    {
        return "Find '". $keyword. "'. Resulted by find in Elasticsearch!";
    }
}

class Adapter implements SearchingInterface
{
    protected $adaptee;

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

    public function search($keyword)
    {
        return $this->adaptee->find($keyword);
    }
}

$elastic = new Adapter(new ElasticSearchSystem());

$client = new Client($elastic);
echo $client->excute('mina');

5. The conclusion

The complexity of the adapter pattern is quite low, but the usability is very high. It’s mean that don’t hesitate to use it to design your system!

Follow the example above, we may think about a way to switch the searching system for the client. Well, if interested, you may want to read about factory pattern


Leave a Reply

Your email address will not be published.