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. {
    4.     public $wp;
    6.     public function __construct(WordPressInterface $wp)
    7.     {
    8.         $this->wp = $wp;
    9.     }
    11.     public function addBlogPost($title, $post_body)
    12.     {
    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);
    19.         // done!
    20.     }
    22. }
    24. interface WordPressInterface
    25. {
    26.     public function login();
    28.     public function setToMaintenanceMode();
    30.     public function post($title, $post_body);
    31. }
    33. class WordPressPublisher implements WordPressInterface
    34. {
    35.     private $loginCredentials;
    37.     public function login()
    38.     {
    39.         // login with WP
    40.         $this->loginCredentials = SomeWPLibrary::login('user', 'pass');
    42.     }
    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);
    49.     }
    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.     }
    58. }
    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.     {
    6.         // don't do anything here - it isn't needed
    7.     }
    9.     public function setToMaintenanceMode()
    10.     {
    11.         // don't do anything here - it isn't needed
    12.     }
    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.     }
    20. }
    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 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

    Subscribe to my spam free newsletter for other Laravel and Vue updates like this

    I never spam, and only email when I have a good in-depth post published on my site (mostly about Laravel and Vue). You can also follow me on social media to get updates.

    webdevetc profile pic

    I've been working as a software developer for many years (with the last few years specialising in Laravel and Vue). I mostly write about PHP and JavaScript/TypeScript on this site. Contact me here. Need to hire a contract software developer in London, UK (or freelance)? Contact me and check my availability.

    Leave a Comment