This repository contains implementations of various design patterns in C++. Design patterns are reusable solutions to commonly occurring problems in software design and development.
- Overview
- Design Patterns Included
- How to Use
- Compilation Instructions
- Pattern Categories
- Contributing
- License
Design patterns help developers write more maintainable, flexible, and reusable code. This collection demonstrates the implementation of classic Gang of Four (GoF) design patterns using modern C++ practices.
- Singleton Pattern - Ensures only one instance of a class exists
- Factory Pattern - Creates objects without specifying exact classes
- Builder Pattern - Constructs complex objects step by step
- Prototype Pattern - Creates objects by cloning existing instances
- Adapter Pattern - Allows incompatible interfaces to work together
- Bridge Pattern - Separates abstraction from implementation
- Composite Pattern - Composes objects into tree structures
- Decorator Pattern - Adds behavior to objects dynamically
- Facade Pattern - Provides simplified interface to complex subsystem
- Flyweight Pattern - Shares objects efficiently to support large numbers
- Proxy Pattern - Provides placeholder/surrogate for another object
- Observer Pattern - Defines one-to-many dependency between objects
- Strategy Pattern - Defines family of algorithms and makes them interchangeable
- Template Method Pattern - Defines skeleton of algorithm in base class
- Iterator Pattern - Provides way to access elements sequentially
- Abstraction Design - Demonstrates abstraction principles
-
Clone the repository:
git clone https://github.com/alloydsa/Low-Level-Design-Patterns.git cd Low-Level-Design-Patterns -
Choose a pattern to study: Each
.cppfile contains a complete implementation of a design pattern with example usage. -
Compile and run:
g++ -std=c++11 "pattern-name.cpp" -o pattern-name ./pattern-name
- C++11 or later
- GCC, Clang, or any modern C++ compiler
# Example: Compile Singleton Pattern
g++ -std=c++11 "singleton design pattern.cpp" -o singleton
./singleton
# Example: Compile Factory Pattern
g++ -std=c++11 "factory design pattern.cpp" -o factory
./factoryWe've included a handy compilation script that compiles all patterns at once:
# Make the script executable (if not already)
chmod +x compile_all.sh
# Compile all patterns
./compile_all.sh
# Run a specific pattern
./compiled/singleton_design_pattern
./compiled/factory_design_patternFocus on object creation mechanisms, trying to create objects in a manner suitable to the situation.
Deal with object composition, creating relationships between objects to form larger structures.
Focus on communication between objects and the assignment of responsibilities between objects.
- Gang of Four Book: "Design Patterns: Elements of Reusable Object-Oriented Software"
- Head First Design Patterns: Great for beginners
- Refactoring Guru: Excellent online resource for design patterns
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingPattern) - Commit your changes (
git commit -m 'Add some AmazingPattern') - Push to the branch (
git push origin feature/AmazingPattern) - Open a Pull Request
- Use meaningful variable and function names
- Include comments explaining the pattern's intent
- Provide example usage in the
main()function - Follow consistent indentation (4 spaces)
- Include output examples in comments
- Pattern files follow the format:
pattern-name design pattern.cpp - Each file is self-contained and executable
- Clear separation between pattern implementation and usage example
Low-Level-Design-Patterns/
├── README.md
├── LICENSE
├── .gitignore
├── compile_all.sh
├── abstraction design.cpp
├── adapter design pattern.cpp
├── bridge design pattern.cpp
├── builder design pattern.cpp
├── composite design pattern.cpp
├── Decorator design pattern.cpp
├── facade design pattern.cpp
├── factory design pattern.cpp
├── flyweight design pattern.cpp
├── iterator design pattern.cpp
├── observer design pattern.cpp
├── prototype design pattern.cpp
├── proxy design pattern.cpp
├── singleton design pattern.cpp
├── strategy design pattern.cpp
├── template design pattern.cpp
└── compiled/ (generated after running compile_all.sh)
Each pattern includes example output to demonstrate its functionality:
// Example from Singleton Pattern
Singleton instance created.
Hello from Singleton!
Same instance: YesCommon Issues:
- Compilation errors: Ensure you're using C++11 or later
- Missing headers: Make sure all
#includestatements are present - Linking issues: Most patterns are header-only and don't require external libraries
For questions, suggestions, or discussions about design patterns, feel free to open an issue in this repository.
This project is licensed under the MIT License - see the LICENSE file for details.
Happy Coding! 🚀
This repository is maintained for educational purposes and demonstrates fundamental design patterns in C++.