Design Patterns are proven solutions to common problems that developers encounter when building software applications. They provide a standardized approach to solving recurring design problems by structuring the code in a proven way.
Design Patterns are not a one-size-fits-all solution for design problems, but they can be highly beneficial in many contexts. They enable developers to quickly and easily address common issues, leading to the creation of more robust and maintainable software applications. In this article, we will explore some of the most commonly used Design Patterns in PHP, along with their practical applications (discussed in other articles).
Design Patterns in PHP are aimed at designers and developers who regularly practice object-oriented programming to create applications based on robust solutions.
We will discuss a few design patterns among the 23 existing ones, as described in the Gang of Four book.
Creational Design patterns : These patterns aim to abstract the mechanisms of object construction. The instantiation mechanisms of concrete classes are encapsulated within these design patterns. In case of changes to the set of concrete classes to be instantiated, the modifications needed in the system will be minimal or even non-existent.
Structural Design Patterns: These patterns abstract the interface of an object or a set of objects from its implementation. In the case of a set of objects, the goal is also to abstract the interface of inheritance or composition relationships present in the set.
Behavioral Design Patterns: These patterns provide solutions for structuring data and objects, as well as organizing interactions by distributing processing and algorithms among objects.
Studying design patterns gives us the opportunity to revisit some fundamental concepts of object-oriented programming (OOP):
- Inheritance
- Encapsulation
- Abstraction
- Polymorphism
For more details, you can visit
It’s essential not to forget the SOLID principles. These five principles, extracted from an article written by the American computer scientist Robert C. Martin in 2000, nicknamed ‘Uncle Bob’ by the international developer community, are crucial:
- Single Responsibility Principle (S): A class should have only one reason to change.
- Open/Closed Principle (O): Software entities should be open for extension but closed for modification.
- Liskov Substitution Principle (L): Subtypes must be substitutable for their base types.
- Interface Segregation Principle (I): Clients should not be forced to depend on interfaces they do not use.
- Dependency Inversion Principle (D): High-level modules should not depend on low-level modules. Both should depend on abstractions.
Design Patterns address frequently encountered design problems in object-oriented programming. They are recognized and proven solutions that stem from the experience of seasoned programmers.
The design patterns are based on best practices of object-oriented programming, and you may already be using them without realizing.
Some Design Patterns include:
- Abstract Factory: Creates objects grouped into families without needing to know the concrete classes.
- Builder: Separates the construction of complex objects from their implementation, allowing clients to create these complex objects with different implementations.
- Factory Method: Introduces an abstract method for creating an object by delegating the actual creation to concrete subclasses.
- Prototype: Allows the creation of new objects by duplicating existing objects called prototypes that have cloning capability.
- Singleton: Ensures that a class has only one instance in memory and provides a unique method returning that instance.
- Adapter: Converts the interface of an existing class into the interface expected by existing clients to enable collaboration.
- Bridge: Separates the conceptual aspects of a class hierarchy from its implementation.
- Composite: Offers a framework for designing object compositions with variable depth based on a tree structure.
- Decorator: Dynamically adds additional features to an object.
- Facade: Groups the interfaces of a set of objects into a unified interface, making the set easier to use.
- Flyweight: Facilitates the sharing of a significant set of fine-grained objects.
- Proxy: Constructs an object that substitutes for another object and controls access to it.
- Chain of Responsibility: Creates a chain of objects so that if one object in the chain cannot handle a request, it can pass it to its successors until one of them handles it.
- Command: Aims to transform a request into an object, facilitating operations like undo, queuing requests, and tracking.
- Interpreter: Provides a framework to give an object representation of the grammar of a language to evaluate expressions written in that language by interpreting them.
- Iterator: Provides sequential access to a collection of objects without clients worrying about the implementation of the collection.
- Mediator: Constructs an object whose purpose is to manage and control interactions within a set of objects without those elements knowing each other.
- Memento: Saves and restores the state of an object.
- Observer: Builds a dependency between a subject and observers so that each modification of the subject notifies the observers so they can update their state. https://www.abetari.com/php-subject-observer/
- State: Allows an object to alter its behavior when its internal state changes.
- Strategy: Adapts the behavior and algorithms of an object based on a need without changing interactions with clients.
- Template Method: Allows deferring some steps of one of an object’s operations to its subclasses.
- Visitor: Constructs an operation to be performed on the elements of a set of objects. New operations can be added without modifying the classes of these objects.
Creational Design Patterns (5): Abstract Factory, Builder, Factory Method, Prototype et Singleton.
Structural Design Patterns (7): Adapter, Bridge, Composite, Decorator, Facade, Flyweight et Proxy
Behavioral Design Patterns (11): Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template Method et Visitor
For this article, we used the book ‘Design Patterns in PHP’ as the main source to explore the theoretical aspects of Design Patterns. This resource was very helpful in understanding the key concepts and practical applications of these design patterns