The Adapter Design Pattern, Explained With PHP

The Adapter Design Pattern, Explained With PHP Thumbnail image

The Adapter Design Pattern, Explained With PHP Table of contents

  1. Example of the adapter interface
  2. Benefits of the adapter design pattern

    The adapter design pattern is an easy to understand design pattern.You use an adapter pattern to convert the interface of a class into another interface which clients expect.

    Example of the adapter interface

    For example, let's say you have some code that let's you publish to a WordPress blog. For this example, I'm going to pretend that the current library you use requires you to login, then set the WordPress blog to maintenance mode (which isn't really a thing to do!), then publish the post. It should return the HTML of the blog post.

    As it is, originally:

    1. class BlogPostPublisher
    2. {
    3.  
    4.     public $wp;
    5.  
    6.     public function __construct(WordPressInterface $wp)
    7.     {
    8.         $this->wp = $wp;
    9.     }
    10.  
    11.     public function addBlogPost($title, $post_body)
    12.     {
    13.  
    14.         // (This is just a silly example. WordPress doesn't work like this, you can just use the XML-RPC protocol to do it in one request.
    15.         $this->wp->login();
    16.         $this->wp->setToMaintenanceMode(); // again, not a thing to do in WP but just for this example
    17.         $this->wp->post($title, $post_body);
    18.  
    19.         // done!
    20.     }
    21.  
    22. }
    23.  
    24. interface WordPressInterface
    25. {
    26.     public function login();
    27.  
    28.     public function setToMaintenanceMode();
    29.  
    30.     public function post($title, $post_body);
    31. }
    32.  
    33. class WordPressPublisher implements WordPressInterface
    34. {
    35.     private $loginCredentials;
    36.  
    37.     public function login()
    38.     {
    39.         // login with WP
    40.         $this->loginCredentials = SomeWPLibrary::login('user', 'pass');
    41.  
    42.     }
    43.  
    44.     public function setToMaintenanceMode()
    45.     {
    46.         // now you are logged in, set it to maintenance mode (not really a think in WP!)
    47.         SomeWPLibrary::setToMaintenanceMode($this->loginCredentials);
    48.  
    49.     }
    50.  
    51.     public function post($title, $post_body)
    52.     {
    53.         // and finally send the actual post to the blog
    54.         // this should return the HTML of the blog post
    55.         return SomeWPLibrary::post($this->loginCredentials, $title, $post_body);
    56.     }
    57.  
    58. }
    59.  
    60. $poster = new BlogPostPublisher(new WordPressPublisher());
    61. $poster->addBlogPost("Hello, world", "Welcome to my blog post");

    But then one day you read about XMLRPC, and you want to update your code base to support this. This doesn't need the login(), or setToMaintenanceMode() code. But when posting via XMLRPC, only the post ID is returned, so an additional step is required the get the HTML of the blog post (like the old way did).

    This isn't really a good real world example, but hopefully it makes sense and is better than the classic type of example for the adapter extension (things like 'a script to bake a cake', then an adapter to allow a machine to do the same thing).

    So, we can use an adapter to use the XMLRPC poster, without having to edit any of the original code.

    1. class XMLRPCPublisher implements WordPressInterface
    2. {
    3.     public function login()
    4.     {
    5.  
    6.         // don't do anything here - it isn't needed
    7.     }
    8.  
    9.     public function setToMaintenanceMode()
    10.     {
    11.         // don't do anything here - it isn't needed
    12.     }
    13.  
    14.     public function post($title, $post_body)
    15.     {
    16.         $post_id = XMLRPCLibrary::post('user', 'pass', $title, $post_body);
    17.         return XMLRPCLibrary::getHtmlForPost($post_id);
    18.     }
    19.  
    20. }
    21.  
    22. $poster = new BlogPostPublisher(new XMLRPCPublisher());
    23. $poster->addBlogPost("Hello, world", "Welcome to my blog post");

    Benefits of the adapter design pattern

    One of the key and main reasons to use this is when your code needs to change its functionality a bit, but you don't want to make any changes to the core base. In the example above, you could pretend that this code supported multiple users and they had the option between selecting uploaders to the (old) WordPress way, maybe a blogger.com interface, and the new WP XML-RPC one. You wouldn't want to have to edit all of the existing code. So create an adapter so you can get the new XML-RPC code working with the existing interfaces.

    Design pattern type: Structural design pattern

    webdevetc profile pic
    webdevetc

    I am a 29 year old freelance backend web developer from London, mostly focusing on PHP and Laravel lately. This is my site - I mostly write about PHP here. Contact me here (especially for any contracting jobs early 2019 in London ;) ).

    Leave a Comment