PHP Professional এবং Zend Certified PHP Engineer (ZCPE ) Course এ সীমিত সংখ্যক আসন বাকি আছে। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করার জন্য অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল নম্বর : ০১৭২২ ৮১ ৭৫ ৯১

Adapter Design Pattern in PHP

Adapter Design Pattern in PHP

PHP adapter Design Pattern

The adapter pattern is a structural design pattern that allows you to re purpose a class with a different interface, allowing it to be used by a system which uses different calling methods.

Adapter: problem & solution

Problem

Solution

  • You want to reuse a library in your application but it doesn’t match your interface
  • You want to normalize multiple libraries to use the same interface
  • The Adapter pattern converts the interface of a class into another interface clients expect
  • Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces

Adapter: considerations

  • Adapter is a common pattern when using third party libraries
  • If you are developing your own libraries, you might want to avoid adapters and use common habits and guidelines
  • Often using in cooperation with other patterns, such as the Strategy pattern
  • Adapt or Modify?
  • Adapter pattern and Proxy pattern are similar, but they have different intents. Adapter is meant to change the interface of an existing object

Real Life Example:

<?php
class SimpleBook {
    private $author;
    private $title;
    function __construct($author_in, $title_in) {
        $this->author = $author_in;
        $this->title  = $title_in;
    }
    function getAuthor() {
        return $this->author;
    }
    function getTitle() {
        return $this->title;
    }
}

class BookAdapter {
    private $book;
    function __construct(SimpleBook $book_in) {
        $this->book = $book_in;
    }
    function getAuthorAndTitle() {
        return $this->book->getTitle().' by '.$this->book->getAuthor();
    }
}

  // client

  writeln('BEGIN TESTING ADAPTER PATTERN');
  writeln('');

  $book = new SimpleBook("Gamma, Helm, Johnson, and Vlissides", "Design Patterns");
  $bookAdapter = new BookAdapter($book);
  writeln('Author and Title: '.$bookAdapter->getAuthorAndTitle());
  writeln('');

  writeln('END TESTING ADAPTER PATTERN');

  function writeln($line_in) {
    echo $line_in."<br/>";
  }

?>

Output:
BEGIN TESTING ADAPTER PATTERN
Author and Title: Design Patterns by Gamma, Helm, Johnson, and Vlissides
END TESTING ADAPTER PATTERN
Note:
In this example we have a SimpleBook class that has a getAuthor() and getTitle() methods. The client, expects a getAuthorAndTitle() method. To “adapt” SimpleBook for testAdapter we have an adapter class, BookAdapter, which takes in an instance of SimpleBook, and uses the SimpleBook getAuthor() and getTitle() methods in it’s own getAuthorAndTitle()method.
Adapters are helpful if you want to use a class that doesn’t have quite the exact methods you need, and you can’t change the orignal class. The adapter can take the methods you can access in the original class, and adapt them into the methods you need.
Adapter: using & abusing

  • Avoid creating adapter of adapters
  • Go to the main library and build the Adapter at the lowest possible level
One comment to “Adapter Design Pattern in PHP”

Leave a Reply

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