A collection of Design Pattern and SOLID Principle examples implemented in C++.
DesignPatterns/
├── BehavioralDesignPattern/
│ ├── Observer.md
│ └── ObserverDesignPattern.cpp
├── CreationalDesign/
│ ├── Builder Design Pattern/
│ │ ├── BuilderDesignPattern.cpp
│ │ ├── BuilderDesignPattern.md
│ │ ├── HTTPRequestBuilder.cpp
│ │ └── HTTPRequestBuilder.md
│ ├── Factory/
│ │ ├── FactoryDesignPattern.cpp
│ │ └── FactoryDesignPattern.md
│ └── SingletonDesign Pattern/
│ ├── DatabaseExample.cpp
│ ├── Logger.cpp
│ └── singletondesignPattern.md
├── ObjectOrientedProgramming/
│ ├── abstraction.cpp
│ ├── Encapsulation.cpp
│ ├── Inheritance/
│ │ ├── MultilevelInheritance.cpp
│ │ ├── MultilevelInheritance.md
│ │ ├── MultipleInheritance.cpp
│ │ ├── MultipleInheritance.md
│ │ ├── SingleInheritance.cpp
│ │ └── SingleInheritance.md
│ └── Polymorphism/
│ ├── Polymorphism.md
│ ├── compile/
│ │ ├── CompileTimePolymorphism.cpp
│ │ └── CompileTimePolymorphism.md
│ └── runtime/
│ ├── RuntimePolymorphism.cpp
│ └── RuntimePolymorphism.md
└── Solid/
├── SingleResponsibilityPrinciple.md
├── singleResponsibilityPrinciple.cpp
├── OpenClosedPrinciple.md
├── OpenClosedPrinciple.cpp
├── LiskovSubstitutionPrinciple.md
├── LiskovSubstitutionPrinciple.cpp
├── InterfaceSegregationPrinciple.md
├── InterfaceSegregationPrinciple.cpp
├── DependencyInversionPrinciple.md
└── DependencyInversionPrinciple.cpp
Each principle has a theory doc (.md) and a C++ implementation (.cpp) showing both a violation and the correct approach.
| # | Principle | Theory | Code |
|---|---|---|---|
| S | Single Responsibility Principle | SRP Theory | SRP Code |
| O | Open/Closed Principle | OCP Theory | OCP Code |
| L | Liskov Substitution Principle | LSP Theory | LSP Code |
| I | Interface Segregation Principle | ISP Theory | ISP Code |
| D | Dependency Inversion Principle | DIP Theory | DIP Code |
| Pattern | Example | Code | Documentation |
|---|---|---|---|
| Builder | Step-by-step object construction | BuilderDesignPattern.cpp | Theory |
| Builder | HTTP Request construction | HTTPRequestBuilder.cpp | HTTP Example |
| Factory | Object creation through factory method | FactoryDesignPattern.cpp | Theory |
| Singleton | Database connection | DatabaseExample.cpp | Theory |
| Singleton | Logger instance | Logger.cpp | Theory |
| Topic | Example | Documentation |
|---|---|---|
| Inheritance | SingleInheritance.cpp | Theory |
| Inheritance | MultilevelInheritance.cpp | Theory |
| Inheritance | MultipleInheritance.cpp | Theory |
| Polymorphism | CompileTimePolymorphism.cpp | Theory |
| Polymorphism | RuntimePolymorphism.cpp | Theory |
Compile with C++17 or later:
g++ -std=c++17 -o output Solid/OpenClosedPrinciple.cpp
./outputAll creational patterns use C++20 features (primarily std::format):
# Builder Pattern - Computer Configuration
g++ -std=c++20 -o builder CreationalDesign/Builder\ Design\ Pattern/BuilderDesignPattern.cpp
./builder
# Builder Pattern - HTTP Request Construction
g++ -std=c++20 -o http_request CreationalDesign/Builder\ Design\ Pattern/HTTPRequestBuilder.cpp
./http_request
# Singleton - Database Example
g++ -std=c++20 -o database CreationalDesign/SingletonDesign\ Pattern/DatabaseExample.cpp
./database
# Singleton - Logger Example
g++ -std=c++20 -o logger CreationalDesign/SingletonDesign\ Pattern/Logger.cpp
./loggerThis project is for educational purposes.