The Decorator Design Pattern in PHP (with easy to follow example)

The Decorator Design Pattern in PHP (with easy to follow example) Thumbnail image

The decorator pattern can be used like this (scroll below it for a short description):

The following example is a simplified example. For a slightly more complicated, but still simple example, please see the 2nd snippet near the bottom of this page. But read through this one first...

  1. interface WebsiteInterface
  2. {
  3.     public function getPrice();
  4.     public function getDescription();
  5. }
  6.  
  7.  
  8. /** This is the base website 'service', it does not accept a WebsiteInterface object in it's constructor */
  9. class BaseWebsite implements WebsiteInterface
  10. {
  11.     public function getPrice()
  12.     {
  13.         return 1000;
  14.     }
  15.     public function getDescription()
  16.     {
  17.         return "    A barebones website";
  18.     }
  19. }
  20.  
  21. /** But this (and all others below it) expect a WebsiteInterface object in it's constructor */
  22. class CustomDesign implements WebsiteInterface
  23. {
  24.     protected $website;
  25.     public function __construct(WebsiteInterface $website)
  26.     {
  27.         $this->website = $website;
  28.     }
  29.     public function getPrice()
  30.     {
  31.         return 2000 + $this->website->getPrice();
  32.     }
  33.     public function getDescription()
  34.     {
  35.         return $this->website->getDescription() . ",\n    and a completely custom design, designed by an in-house designer";
  36.     }
  37. }
  38.  
  39. class WordPressBlog implements WebsiteInterface
  40. {
  41.     protected $website;
  42.     public function __construct(WebsiteInterface $website)
  43.     {
  44.         $this->website = $website;
  45.     }
  46.     public function getPrice()
  47.     {
  48.         return 750 + $this->website->getPrice();
  49.     }
  50.     public function getDescription()
  51.     {
  52.         return $this->website->getDescription() . ",\n    and setting everything up with a WordPress blog";
  53.     }
  54. }
  55.  
  56. class ContactForm implements WebsiteInterface
  57. {
  58.     protected $website;
  59.     public function __construct(WebsiteInterface $website)
  60.     {
  61.         $this->website = $website;
  62.     }
  63.     public function getPrice()
  64.     {
  65.         return 150 + $this->website->getPrice();
  66.     }
  67.     public function getDescription()
  68.     {
  69.         return $this->website->getDescription() . ",\n    and adding a contact form";
  70.     }
  71. }
  72.  
  73.  
  74. class YearHosting implements WebsiteInterface
  75. {
  76.     protected $website;
  77.     public function __construct(WebsiteInterface $website)
  78.     {
  79.         $this->website = $website;
  80.     }
  81.  
  82.     public function getPrice()
  83.     {
  84.         return (12 * 30) + $this->website->getPrice();
  85.     }
  86.  
  87.     public function getDescription()
  88.     {
  89.         return $this->website->getDescription() . ",\n    including a year's hosting";
  90.     }
  91. }
  92.  
  93. // basic website:
  94.  
  95. // Let's see the price / desc of the basic website. This is just like a normal bit of code:
  96. echo "=== BASIC WEBSITE ===\n";
  97. $basic_website = new BaseWebsite();
  98. echo "Cost: " . $basic_website->getPrice() . "\n";
  99. echo "Description of all included services: \n" . $basic_website->getDescription() . "\n";
  100.  
  101.  
  102. // but now, let's say we want to get the price of a website with a custom design. Here is how we can get it all working:
  103. echo "=== BASIC WEBSITE + CUSTOM DESIGN  ===\n";
  104.  
  105. $basic_and_custom_design = new CustomDesign(new BaseWebsite());
  106. echo "Cost: " . $basic_and_custom_design->getPrice() . "\n";
  107. echo "Description of all included services: \n" . $basic_and_custom_design->getDescription() . "\n";
  108.  
  109. echo "=== BASIC WEBSITE + CUSTOM DESIGN + WP + CONTACT FORM + HOSTING FOR A YEAR ===\n";
  110.  
  111. $basic_and_custom_design = new YearHosting(new ContactForm(new WordPressBlog(new CustomDesign(new BaseWebsite()))));
  112. echo "Cost: " . $basic_and_custom_design->getPrice() . "\n";
  113. echo "Description of all included services: \n" . $basic_and_custom_design->getDescription() . "\n";

The output:

=== BASIC WEBSITE ===
Cost: 1000
Description of all included services: 
    A barebones website
=== BASIC WEBSITE + CUSTOM DESIGN  ===
Cost: 3000
Description of all included services: 
    A barebones website,
    and a completely custom design, designed by an in-house designer
=== BASIC WEBSITE + CUSTOM DESIGN + WP + CONTACT FORM + HOSTING FOR A YEAR ===
Cost: 4260
Description of all included services: 
    A barebones website,
    and a completely custom design, designed by an in-house designer,
    and setting everything up with a WordPress blog,
    and adding a contact form,
    including a year's hosting

A description of the decorator design pattern in PHP: The decorator pattern is a good way to be able to easy extend your application with if you need to change how a script will handle things at runtime. I could have achieved the same output by making a bunch of methods on the main class with things like public function add_hosting() { $this->price += 200; $this->description .= " Add hosting..."; }. But that is not a good idea. Using the decorator pattern is much more extendable, cleaner code, follows SOLID principles.

Using the decorator pattern means that you can easily add new features, without having the change the main or existing class(es). In the example above, it would be very trivial to add a new service called "TransferSiteFromOldHost", and add that to an order.

I simplified the example above, but it is actually a good idea to actually replace the interface with an abstract class, which includes abstract functions. The main base service class would only implement the interface - the others would extend the abstract class (which itself does implement the interface). Here is an example (the output would be the exact same):

  1. interface WebsiteInterface
  2. {
  3.     public function getPrice();
  4.     public function getDescription();
  5. }
  6.  
  7. abstract class WebsiteFeature implements WebsiteInterface
  8. {
  9.  
  10.     protected $website;
  11.     public function __construct(WebsiteInterface $website)
  12.     {
  13.         $this->website = $website;
  14.     }
  15.  
  16.     abstract public function getPrice(); // must be implemented by child class
  17.     abstract public function getDescription(); // must be implemented by child class
  18. }
  19.  
  20. /** This only implements the interface (does not extend the abstract function, so does not have the constructor) */
  21. class BaseWebsite implements WebsiteInterface
  22. {
  23.     public function getPrice()
  24.     {
  25.         return 1000;
  26.     }
  27.     public function getDescription()
  28.     {
  29.         return "    A barebones website";
  30.     }
  31. }
  32.  
  33. /** But this (and all others below it) extends the abstract class, so the constructor is there. They must implement the abstract methods too.*/
  34. class CustomDesign extends WebsiteFeature
  35. {
  36.  
  37.     public function getPrice()
  38.     {
  39.         return 2000 + $this->website->getPrice();
  40.     }
  41.     public function getDescription()
  42.     {
  43.         return $this->website->getDescription() . ",\n    and a completely custom design, designed by an in-house designer";
  44.     }
  45. }
  46.  
  47. class WordPressBlog extends WebsiteFeature
  48. {
  49.     protected $website;
  50.     public function __construct(WebsiteInterface $website)
  51.     {
  52.         $this->website = $website;
  53.     }
  54.     public function getPrice()
  55.     {
  56.         return 750 + $this->website->getPrice();
  57.     }
  58.     public function getDescription()
  59.     {
  60.         return $this->website->getDescription() . ",\n    and setting everything up with a WordPress blog";
  61.     }
  62. }
  63.  
  64. class ContactForm extends WebsiteFeature
  65. {
  66.     protected $website;
  67.     public function __construct(WebsiteInterface $website)
  68.     {
  69.         $this->website = $website;
  70.     }
  71.     public function getPrice()
  72.     {
  73.         return 150 + $this->website->getPrice();
  74.     }
  75.     public function getDescription()
  76.     {
  77.         return $this->website->getDescription() . ",\n    and adding a contact form";
  78.     }
  79. }
  80.  
  81.  
  82. class YearHosting extends WebsiteFeature
  83. {
  84.     protected $website;
  85.     public function __construct(WebsiteInterface $website)
  86.     {
  87.         $this->website = $website;
  88.     }
  89.  
  90.     public function getPrice()
  91.     {
  92.         return (12 * 30) + $this->website->getPrice();
  93.     }
  94.  
  95.     public function getDescription()
  96.     {
  97.         return $this->website->getDescription() . ",\n    including a year's hosting";
  98.     }
  99. }

Again, this example was simplified. In the real world you wouldn't be just returning strings like this. But hopefully it gets the idea across.

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