Skip to content

MarioGalindoQ/Modern-CPP-Design-Patterns

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

591 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Modern C++ Design Patterns & OO Principles

Author: Mario Galindo Queralt, Ph.D.

C++ Standard License Patterns

Welcome to a comprehensive educational repository of 38 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).

--- Authenticity & Heritage ---

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.

--- Overview ---

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 Assignment
    • 5:MA - Move Assignment
    • 6:De - Destructor
    • 7:PC - Parametric Constructor

For more information about the author's "Rule of Seven", refer to the 004_Rule_of_Seven.txt file.

--- What this Repository Covers ---

The following patterns and techniques are implemented with production-grade logic:

  • Creational: Builder, Factory Method, Abstract Factory, Prototype, Singleton, Lazy Initialization, Named Constructors.
  • Structural: Adapter, Bridge, Composite, Decorator, Facade, Flyweight, Proxy, Mixin, Register, Module Loader.
  • Behavioral: Chain of Responsibility, Command, Interpreter, Iterator, Mediator, Memento, Observer, Finite State Machine, Strategy, Template Method, Visitor.
  • Modern C++ (Architecture, Concurrency & Performance): Framework, Null Object, std::variant Dispatch, CRTP, Deducing This (C++23), Error Handling (std::expected), Thread Pools, Component Registry (DOD), Type Erasure, Expression Templates (Loop Fusion).

--- Why This Helps ---

  • Job Interviews: Review C++ idioms and patterns before technical evaluations.
  • Clean Code: Understand how SOLID principles look in actual, modern C++ code.
  • Architectural Wisdom: Compare different ways to solve the same problem (e.g., GoF vs. Modern Variant).
  • Object Ownership: Learn to manage object lifetimes without manual delete calls.

--- Repository Structure ---

The project is organized into four logical blocks:

  1. Fundamental Principles: Detailed analysis of SOLID, IoC, Hollywood Principle, and more (see 002_OO_Principles.txt).
  2. Creational Patterns: Managing object instantiation and lifecycle.
  3. Structural Patterns: Compiling classes and objects into larger, flexible structures.
  4. Behavioral Patterns: Handling communication between objects and algorithmic distribution.

--- Execution Traceability ---

To provide immediate verification and aid in understanding the program flow, every pattern directory includes an output.txt file. This file contains the exact terminal output from a successful execution. It is particularly useful for tracing the memory management logs (The "Rule of Seven") and verifying that your local compilation results match the author's intended behavior.

--- How to Compile ---

Each example is self-contained. To ensure the best performance and standard compliance, it is recommended to compile using C++20/23.

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 the 003_How_to_compile.txt file.

--- License & Usage ---

This repository is provided as an open educational resource under the following terms:

  • Free Use: You are free to use, copy, modify, and distribute these examples for personal, educational, or commercial purposes.
  • Mandatory Attribution: If you redistribute, publish, or use this code in any public project, course, or publication, you must provide a clear reference and a link back to this original repository:

https://github.com/MarioGalindoQ/Modern-CPP-Design-Patterns

--- Philosophical Note ---

"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.

UML Symbology Reference

The class diagrams in this repository use Mermaid notation to represent the relationships between classes. The following diagram explains the arrows and 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
Loading