Skip to main content
Design patterns

Software design patterns are crucial tools in the field of software development, providing developers with proven solutions to common programming challenges. These patterns encapsulate the collective wisdom and best practices of experienced software engineers, enabling the creation of robust, scalable, and maintainable code. In this article, we will explore the importance of design patterns in programming, their benefits, and some popular examples of design patterns using Drupal in most examples, because I try to bring Drupal into everything. 

What are Design Patterns?

Design patterns are reusable solutions to recurring design problems that arise during software development. They are not specific implementations or ready-to-use code snippets but rather high-level concepts and guidelines that address common issues. These patterns help developers streamline the design process, promote code reusability, and improve the overall quality of software systems. Design patterns are also formalized best practices that the programmer can use to solve common problems when designing an application or system. 

Drupal, while using the Object-oriented design design patterns, is a very unique case for this because Drupal was first built on PHP 4 which did not have any classes or support for OOPs. Drupal grew along with PHP and by Drupal 7 there were a lot of OOPs paradigms despite not being fully object oriented or MVC. From Drupal 8 onward it started being completely object oriented with most logic being moved into classes (Entities, Events, Plugins, etc). Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Patterns that imply mutable state may be unsuited for functional programming languages. Some patterns can be rendered unnecessary in languages that have built-in support for solving the problem they are trying to solve, and object-oriented patterns are not necessarily suitable for non-object-oriented languages which was the case initially for PHP, but later version addressed this by bringing in more OOP.

Why are Design Patterns Used?

  1. Encouraging Best Practices: Design patterns embody proven software engineering principles and best practices. By following these patterns, developers can adhere to established guidelines and conventions, ensuring their code is consistent, readable, and maintainable. This consistency is particularly beneficial when working collaboratively or when maintaining code over an extended period.
  2. Enhancing Code Reusability: Design patterns enable the creation of modular and reusable code components. Instead of reinventing the wheel for each new project, developers can leverage design patterns to solve common problems efficiently. Reusable code promotes efficiency, reduces development time, and helps minimize the likelihood of bugs and errors.
  3. Promoting Scalability and Flexibility: As software systems grow and evolve, the ability to scale and adapt becomes paramount. Design patterns assist in designing flexible architectures that can accommodate future changes without significant code restructuring. These patterns make it easier to add new features, modify existing ones, and accommodate shifting requirements.
  4. Improving Maintainability: Design patterns contribute to software maintainability by separating concerns and encapsulating specific functionalities within distinct components. This modularity facilitates code maintenance, debugging, and testing, as changes made in one area are less likely to impact the entire system. Moreover, design patterns enhance code readability and comprehensibility, making it easier for developers to understand and modify codebases.

Types of Design Patterns

Design patterns had originally been categorized into 3 sub-categories based on what kind of problem they solve. Creational patterns assist in creating objects, structural patterns help organize classes and objects, and behavioral patterns focus on communication patterns between objects. Each category of design patterns addresses specific aspects of software design and contributes to creating well-structured, modular, and maintainable systems.

When you are building a new framework, CMS or OS, you need to know what are the priorities or main features that this system needs and you must select those design patterns to go into your system. Taking the example of Drupal; Drupal focuses on being Object Oriented, modular, scalable, pluggable, etc. And hence, Drupal uses design patterns like Singleton pattern, Decorator pattern, Plugin pattern, Factory pattern and so on. You can read more about it here

Taking a single example, Drupal wanted to keep the flexibility of "hooks" in an object oriented approach; This is where the Observer pattern helped. Events are "observed" and can be "subscribed" to. In Angular, it is more easily understandable because they use the word observable quite literally in their "Observables" which can be subscribed to. 

Let's look at the different types of Design patterns.

  1. Creational Patterns: These patterns focus on object creation mechanisms, abstracting the process of object instantiation. Examples include the Singleton pattern (ensuring only one instance of a class exists), the Factory pattern (delegating object creation to subclasses or factory methods).
    Drupal uses the Singleton pattern to ensure that certain objects are instantiated only once throughout the system. For example, Drupal's database connection and caching system use the Singleton pattern to provide a single instance of the respective objects. Similarly, Drupal uses the Factory pattern to create objects in a flexible and configurable way. Drupal's dependency injection container, known as the Service Container, is an example of a factory pattern that provides services to different parts of the system.
  2. Structural Patterns: Structural patterns deal with the composition of classes and objects to form larger structures. The Adapter pattern (enabling the interaction between incompatible interfaces), the Decorator pattern (adding behavior dynamically to objects), and the Composite pattern (treating objects and groups of objects uniformly) are common examples.
    Drupal uses the Decorator pattern to modify or extend the behavior of objects dynamically. In Drupal, decorators are used to add functionality to entities, forms, and other objects without changing their original implementation at runtime; One of the reasons why decorator pattern can be applied to only public methods.
  3. Behavioral Patterns: These patterns address the communication and interaction between objects, defining the behavior and responsibilities of classes. The Observer pattern (maintaining a list of dependents that are notified of changes), the Strategy pattern (encapsulating interchangeable algorithms), the Command pattern (encapsulating requests as objects), and the Chain of responsibility pattern are widely used behavioral patterns.
    Drupal uses the Chain of Responsibility pattern to handle requests or events through a chain of objects, where each object in the chain has the option to handle the request or pass it on to the next object. Drupal's menu and routing systems are examples of the Chain of Responsibility pattern, where different objects in the chain handle requests based on their configuration.

In my reading, I also came across "Concurrency patterns" which is outside of the initial 3 sub-categories of design patterns. Here you can find many patterns that are used in modern programming languages like Go and Rust and some concepts used in OS Kernels like mutexes, semaphores and locks. I'll write a more detailed article about this because its quite interesting. 

In short, Design patterns provide invaluable solutions to recurring design challenges in software development. By leveraging the collective knowledge and experience of software engineers, developers can enhance the quality, reusability, and scalability of their codebases. Design patterns encourage best practices, promote code reusability, enable flexible architectures, and improve software maintainability. By incorporating design patterns into their development process, programmers can build robust and adaptable software systems that are easier to understand, modify, and extend over time.

Featured Image: iStock/as creative atelier

x

Work

Therefore logo
80 Atlantic Ave, Toronto, ON Canada
Email: hello@therefore.ca
Call us: +1 4166405376
Linkedin

Let us know how we can help!