A Journey Towards Drupal 8 Development Mastery (3): Design patterns

This is the third part in the series in which I aim to explore mastering the basics of Drupal 8 development.  In the first part, I outlined the road map that will be my guide in this endeavor. In the second, I reviewed the road map to make things a little clearer. In this third part, I take on the first topic from my road map: Modern PHP Programming: Programming Patterns

So this is where things starts to get a bit more interesting.

A Journey Towards Drupal 8 Development Mastery: Design patterns

This part is also the start of the first section of the road map, The Technology Stack section. This section deals with all the topics that are traditionally foreign to Drupal, but on which Drupal 8 is now based. As such all posts of this section will generally take the following structure:

  • Introduction, descriptions, definitions and references where appropriate
  • Importance/significance (especially as it applies to Drupal 8).
  • Example codes if necessary

The hierarchy of this part is:

  • The Technology Stack
    • Modern PHP Programming
      • Programming Patterns  

Whereas developing for Drupal 7 and earlier versions of Drupal required mostly legacy PHP procedural programming style, developing for Drupal 8 now requires a practical knowledge of lots of modern programming ideas. These are as listed in our road map in the first series, and extracted and repeated below.

Modern PHP programming

  • Programming patterns
  • Factory methods
  • Dependency injection
  • Namespaces & PSR-0
  • Modern Object-Oriented PHP
  • Classes, objects, inheritance
  • Late static binding

This post is about the first item from the list, Programming Patterns. I will address the following questions briefly:

  • What are programming or design patterns?
  • Why are they important?
  • And what design patterns are applicable to Drupal 8?

I will also list link to resources elsewhere where the topic is addressed more thoroughly.

Subsequent series will do same with next items from the list.

What Are Programming or Design Patterns?

Simply, design patterns are generally accepted solutions to common problems that occurs in software development. It implies a well established way of structuring codes that addresses a particular commonly occurring challenge in software development. These patterns are widely used, well tested and confirmed to be an optimal solution for the problem in question.

Why Are Design Patterns Important?

For one it provides a reliable and common way of solving a particular group of problems. As a result you do not need to workout your own solution that is most likely to be less optimal. This also saves you time.

Also this allows others familiar with the pattern to easily understand your codes without too much effort.

On a large scale it makes working with large application projects, like Drupal, a whole lot easier for every one involved. Imagine a world where the Drupal core is created by various contributors using their own custom coding styles. Things will be a mess and Drupal wouldn't be as popular.


For more detailed introductions or further information to PHP Design Patterns you should check out the following excellent resources:

Common Design Patterns Used In Drupal 8

While there are a lot of design pattern already established within Drupal before Drupal 8, Drupal 8 now consists of a lot of patterns that where not available in previous versions of Drupal. Some of these new patterns completely replaces some older ones, while others introduces completely new features to Drupal.

The three obvious patterns used in Drupal 8 includes:

  • Object-Oriented Programming Pattern (OOP)
  • Factory Pattern
  • Late Static Binding

OOP is not really a single pattern, but a completely radical way of conceptualizing and structuring codes that goes way beyond just design patterns. It is the basis for a lot of popular software design patterns in use today, including those used in Drupal 8. It was introduced in Drupal 7, but it was not used extensively and it was not required. The situation in Drupal 8 is now different, it is used widely and it is required.

I will be introducing the above three patterns in their own separate posts.

While an extensive knowledge of design patterns are not necessary for Drupal 8 development mastery, a basic general knowledge and knowledge of specific patterns as they apply to Drupal 8 are still needed.

If you have managed to work through a few of the introductory references above, I think we now have enough knowledge to move on to the next topic.

Update: Some while after I published this post I came across another article, from DrupalWatchDog.com, that introduces design patterns much better than I did here. It's worth checking out: Design Patterns of Drupal.

Next: Modern PHP Programming:  Factory methods


Add new comment