Identify the design pattern in Drupal

Rahul Kumar
3 min readAug 3, 2021

Basically Design Pattern is a general solution to a commonly occurring problem in software Design. As a definition of adept code, it should be reusable, maintainable and scalable.

Since Drupal 8 adopted modern PHP and OOP concepts, organizing your codes based on design patterns would help you to achieve the definition of efficient code. So let’s explore some design pattern implemented in Drupal 8.

Dependency Injection is a software design approach that allows avoiding hard-coding dependencies and makes it possible to change the dependencies both at runtime and compile time.

It’s a procedure where one object supplies the dependencies of another object.

Arguments in services.yml is an example for dependency injection in Drupal 8

Dependency Injection Design Pattern — Drupal 8
https://www.drupal.org/project/watchdog_kafka/

arguments: Argument(s) to be used with the factory method (in case of ‘factory_class’) or with the class constructor (in case of ‘class’). A ‘@’ indicates another service, and then what is put in here is the name of the service, as defined in its services.yml file. An argument wrapped in ‘%’ characters represents a parameter, which can be defined in the ‘parameters’ section of the file

Here ConfigFactory and LogMessageParser added as the dependency of service kakfka_logging.

Decorator is a structural pattern that allows adding new behaviors to objects dynamically by placing them inside special wrapper objects.

Using decorators you can wrap objects countless number of times since both target objects and decorators follow the same interface. The resulting object will get a stacking behavior of all wrappers.

Likewise in workspaces module, decorator is defined in workspaces.services.yml -

Decorator Design Pattern — Drupal 8

then it extend it in the src/EntityQuery/QueryFactory.php

/**
* Workspaces-specific entity query implementation.
*/
class QueryFactory extends BaseQueryFactory {

Factory pattern is used to build an object. That’s right — build an object and not create an object. When we build the object, we first create it and then initialize it. Usually, it requires to apply certain logic and perform multiple steps.

It’s a creational design pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

Likewise in Drupal 8 — CacheFactory class defines the cache backend factory in core/lib/Drupal/Core/Cache/CacheFactory.php

class CacheFactory implements CacheFactoryInterface, ContainerAwareInterface {

Using this Cachefactory, we can get the cache backend object associated with the specified bin.

Factory Design Pattern

Singleton is a class that has one instance, It provides a global access point to it, Following code will explain about singleton concept.
In order to prevent the explicit creation of objects from the Singleton pattern class, we’ll have to define a private constructor.

In Drupal \Drupal\Core\Site\Settings is a singleton class. It provide read only settings that are initialized with the class.

Singleton Class in Drupal 8

And it is getting initialised in core/lib/Drupal/Core/DrupalKernel.php

Singleton Class

More details related to this class is given here —
https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Site%21Settings.php/class/Settings/8.8.x

Keep Learning… Keep Sharing…
🙏

--

--

Rahul Kumar

Programmer | Freelancer | Thinker | Open Source | Tech Mantra