Welcome to a comprehensive educational repository of 34 Design Patterns and C++ Idioms. This project represents a bridge between classic software architecture (the Gang of Four legacy with C++98/11) and Modern C++ best practices (with C++14/17/20/23).
Check out our Interactive Design Patterns Index for community debate and Q&A.
This repository is a curated collection of design patterns developed, tested, and refined by the author over nearly four decades of professional experience in C++.
While modern tools (including AI) have been used to aid in organizing documentation, ensuring structural consistency, and formatting the repository for GitHub, the core architectural designs, logic, and implementations are the author's original work. This code is not a generic output of a machine; it is the result of years of real-world engineering practice and pedagogical refinement. Every line is verified and deliberately written to reflect established software engineering principles.
This repository is designed as a masterclass for developers who want to master object-oriented design in C++. Every example has been carefully crafted to demonstrate not just the pattern's logic, but also high-level engineering concerns such as:
- Memory Management: Full use of RAII, std::unique_ptr, and std::shared_ptr.
- Performance: Demonstrations of the "Zero-Overhead Principle" using Static Polymorphism (CRTP) and Mixins.
- Modern Features: Extensive use of std::variant, std::visit, if constexpr, and C++20 Abbreviated Templates.
- The "Rule of Seven": A unique educational approach to tracing an object's
full lifecycle:
- 1 DC: Default Constructor
- 2 CC: Copy Constructor
- 3 MC: Move Constructor
- 4 CA: Copy Assigment
- 5 MA: Move Assigment
- 6 De: Destructor
- 7 PC: Particular Constructor
For more information about author's "Rule of Seven", refer to '004_Rule_of_Seven.txt' file
The project is organized into four logical blocks:
- Fundamental Principles: Detailed analysis of SOLID, IoC, Hollywood Principle, and more (see '002_OO_Principles.txt' file).
- Creational Patterns: Managing object instantiation and lifecycle (Builder, Factory, Singleton, etc.).
- Structural Patterns: Compiling classes and objects into larger, flexible structures (Bridge, Decorator, Proxy, Mixins).
- Behavioral Patterns: Handling communication between objects and algorithmic distribution (Command, Interpreter, Observer, Visitor).
Each example is self-contained. To ensure the best performance and standard compliance, it is recommended to compile using C++20 or higher.
The author uses a custom gcc3 alias for optimized compilation:
c++ -std=c++23 -O3 -Wfatal-errors -Wall -Wextra -Wpedantic program.cpp -o program -pthread
For detailed instructions, refer to '003_How_to_compile.txt' file.
This repository is provided as an open educational resource.
- Free Use: You are free to use, copy, modify, and distribute these examples for personal, educational, or commercial purposes.
- Attribution: If you find this material useful or use it in your own projects/courses, I would greatly appreciate a mention or a link back to this repository.
Author: Mario Galindo Queralt, Ph.D.
"The code is the vehicle, but the comments are the gold."
This repository places a high value on internal documentation. You will find extensive comments explaining the "why" behind each architectural decision, the trade-offs of different implementations, and the evolution of the C++ language over the last four decades.
The class diagrams in this repository use Mermaid notation to represent the relationships between classes. The following diagram explains the arrows an multiplicities used:
---
config:
class:
hideEmptyMembersBox: true
---
classDiagram
direction LR
A --|> B : A is a B
C ..|> D : C implements D
E *-- F : E Has an F (owner)
G *-- "n" H : G Has several Hs (owner)
J o-- K : J Has an K (not owner)
L o-- "n" M : L Has several Ms (not owner)
N --> P : N reference P
Q ..> R : Q uses R
S <..> T : Bi-directional reference
U .. V : U Interacts with V