This repository contains a comprehensive guide to the 23 Gang of Four (GoF) design patterns implemented in C++.
- Complete documentation of all 23 design patterns
- Real-world analogies for easy understanding
- Modern C++ implementations using C++11/14/17 features
- Practical examples with usage demonstrations
- Bad usage examples to show common mistakes
Focus on object creation mechanisms:
- Singleton
- Factory Method
- Abstract Factory
- Builder
- Prototype
Focus on object composition and relationships:
- Adapter
- Bridge
- Composite
- Decorator
- Facade
- Flyweight
- Proxy
Focus on communication between objects:
- Chain of Responsibility
- Command
- Interpreter
- Iterator
- Mediator
- Memento
- Observer
- State
- Strategy
- Template Method
- Visitor
- Read the Documentation: Open
design_patterns.mdto explore all patterns - Compile Examples: Each pattern includes complete, compilable C++ code
- Learn by Example: Study the real-world analogies and implementations
All code examples are written in modern C++ and can be compiled with:
# For individual pattern examples
g++ -std=c++17 -Wall -Wextra -o pattern_example pattern_code.cpp
# Example for Singleton pattern
g++ -std=c++17 -Wall -Wextra -o singleton singleton.cpp- C++11 or later compiler (GCC, Clang, MSVC)
- Standard library support
Start with these fundamental patterns:
- Singleton - Learn about controlled instantiation
- Factory Method - Understand object creation abstraction
- Observer - Grasp event-driven programming
- Strategy - See algorithm encapsulation
Progress to these structural patterns:
- Adapter - Interface compatibility
- Decorator - Dynamic behavior addition
- Facade - Simplifying complex systems
- Composite - Tree structures
Master these complex behavioral patterns:
- Command - Request encapsulation
- State - Behavior based on state
- Visitor - Operations on object structures
- Mediator - Object interaction management
- Reusability: Proven solutions to common problems
- Maintainability: Well-structured, modular code
- Communication: Common vocabulary for developers
- Best Practices: Industry-standard approaches
| Problem | Recommended Pattern |
|---|---|
| Need only one instance | Singleton |
| Create objects without specifying types | Factory Method |
| Add behavior without changing classes | Decorator |
| Simplify complex interfaces | Facade |
| Notify multiple objects of changes | Observer |
| Encapsulate algorithms | Strategy |
| Handle requests in chain | Chain of Responsibility |
| Undo operations | Command + Memento |
- Modern C++: Uses smart pointers, RAII, and move semantics
- Thread Safety: Includes thread-safe implementations where relevant
- Exception Safety: Proper error handling and resource management
- const Correctness: Appropriate use of const qualifiers
- SOLID Principles: Demonstrates good object-oriented design
- Clone or download this repository
- Open
design_patterns.mdin your favorite Markdown viewer - Copy any pattern's code to test it locally
- Modify examples to fit your specific use cases
- Gang of Four Book - Original design patterns reference
- Refactoring Guru - Interactive pattern explanations
- C++ Core Guidelines - Modern C++ best practices
Feel free to:
- Report issues or suggest improvements
- Add more real-world examples
- Contribute additional patterns or variations
- Improve documentation clarity
This documentation is provided for educational purposes. Feel free to use and modify the code examples for your projects.
Happy Coding! 🎉
Remember: Design patterns are tools, not rules. Use them when they solve real problems, not just because you can.