A comprehensive, well-organized repository of software design patterns
with clean implementation examples, before/after comparisons, and detailed documentation.
π§ Creational Β Β·Β ποΈ Structural Β Β·Β π‘ Behavioral Β Β·Β π Quick Start Β Β·Β π€ Contributing
Design Patterns are reusable solutions to commonly occurring problems in software design. They represent battle-tested development paradigms that speed up development and improve code quality.
This repository is a structured learning resource and reference guide covering all 23 Gang of Four (GoF) design patterns, organized into three categories β each with clear explanations, real-world use cases, and practical C# implementations.
| Category | Focus | Patterns |
|---|---|---|
| π§ Creational | How objects are created | 5 |
| ποΈ Structural | How objects are composed | 7 |
| π‘ Behavioral | How objects communicate | 11 |
| Total GoF Patterns | 23 |
Control the creation process β decouple the system from how its objects are created, composed, and represented.
| Pattern | Intent | Common Use Cases |
|---|---|---|
| π² Singleton | One instance, global access point | DB connections, config managers |
| π Factory Method | Delegate creation to subclasses | UI components, plugin loaders |
| π’ Abstract Factory | Create families of related objects | Cross-platform UI kits |
| π¨ Builder | Construct complex objects step-by-step | Query builders, document generators |
| π₯ Prototype | Clone existing objects | Game entities, undo snapshots |
β Reach for Creational patterns when you need to:
- Hide the complexity of object creation
- Ensure only one instance of a class exists
- Create families of related objects together
- Build objects with many optional parameters
Simplify structure by identifying simple ways to realize relationships between entities.
| Pattern | Intent | Common Use Cases |
|---|---|---|
| π Adapter | Make incompatible interfaces work together | Third-party library wrappers |
| π Bridge | Decouple abstraction from implementation | Cross-platform rendering |
| π³ Composite | Treat single objects and groups uniformly | File systems, UI trees |
| π¨ Decorator | Add behavior to objects dynamically | Logging, caching, auth layers |
| π Facade | Simplified interface to complex subsystems | API clients, SDK wrappers |
| π« Flyweight | Share common state to reduce memory | Game tiles, text rendering |
| π‘οΈ Proxy | Controlled access to another object | Lazy loading, access control |
β Reach for Structural patterns when you need to:
- Compose objects into larger, flexible structures
- Wrap incompatible interfaces without changing them
- Add new responsibilities dynamically at runtime
- Minimize memory footprint through shared state
Define how objects interact and distribute responsibility β making systems more flexible and maintainable.
| Pattern | Intent | Common Use Cases |
|---|---|---|
| βοΈ Chain of Responsibility | Pass requests along a handler chain | Middleware, validation pipelines |
| π Command | Encapsulate requests as objects | Undo/redo, task queues |
| π Iterator | Sequential access to collection elements | Custom collections, data streams |
| π€ Mediator | Centralize complex communications | Chat systems, air traffic control |
| πΈ Memento | Capture and restore object state | Undo history, snapshots |
| ποΈ Observer | Notify subscribers of state changes | Event systems, reactive UIs |
| π State | Change behavior based on internal state | Order workflows, game states |
| β‘ Strategy | Define a family of interchangeable algorithms | Sorting, payment methods |
| π Template Method | Define algorithm skeleton in base class | Data parsers, report generators |
| πΆ Visitor | Perform operations on object structures | Compilers, document export |
| π£οΈ Interpreter | Interpret sentences in a language | DSLs, expression evaluators |
β Reach for Behavioral patterns when you need to:
- Decouple senders from receivers of requests
- Define algorithms that can vary independently
- Allow objects to notify others without tight coupling
- Implement complex conditional logic cleanly
git clone https://github.com/devmohamedsakr-prog/Design-Patterns.git
cd Design-Patterns# Example: explore the Singleton implementation
cd Creational/Singleton/CurrencyConverter/Afterdotnet restore # Restore NuGet packages
dotnet build # Build the project
dotnet test # Run the test suite
dotnet run # Run the applicationEach pattern folder follows a consistent layout:
PatternName/
βββ Before/ β The problem (without the pattern)
β βββ app.cs
β βββ README.md
β
βββ After/ β The solution (with the pattern applied)
βββ src/ β Implementation files
βββ Tests/ β Unit & integration tests
βββ docs/ β Pattern documentation
Design-Patterns/
β
βββ π Creational/
β βββ Singleton/
β βββ Factory/
β βββ AbstractFactory/
β βββ Builder/
β βββ Prototype/
β
βββ π Structural/
β βββ Adapter/
β βββ Bridge/
β βββ Composite/
β βββ Decorator/
β βββ Facade/
β βββ Flyweight/
β βββ Proxy/
β
βββ π Behavioral/
β βββ ChainOfResponsibility/
β βββ Command/
β βββ Iterator/
β βββ Mediator/
β βββ Memento/
β βββ Observer/
β βββ State/
β βββ Strategy/
β βββ TemplateMethod/
β βββ Visitor/
β βββ Interpreter/
β
βββ π README.md
βββ π RELEASE_NOTES.md
Not sure which pattern to use? Use these decision points:
- You need to control how objects are instantiated
- Object creation logic is becoming too complex or scattered
- You want to enforce a single instance across the system
- You're building families of related objects
- You need to combine objects into larger, flexible structures
- You're working with legacy or third-party interfaces you can't change
- You want to extend behavior without subclassing
- You need to reduce memory by sharing common state
- You need to decouple who sends a request from who handles it
- You want behavior to change at runtime based on state
- You're building an event-driven or reactive system
- You need to make algorithms interchangeable
Contributions are welcome β new implementations, improvements, and documentation all count.
# 1. Fork the repo and clone your fork
git clone https://github.com/YOUR-USERNAME/Design-Patterns.git
# 2. Create a feature branch
git checkout -b feature/add-observer-pattern
# 3. Make your changes, then commit
git commit -m "feat: add Observer pattern with event system example"
# 4. Push and open a Pull Request
git push origin feature/add-observer-patternDo β
- Write clear, commented code with real-world context
- Include a
Before/andAfter/for each pattern - Add unit tests where applicable
- Update or create documentation in
docs/
Don't β
- Submit untested or uncommented code
- Add content unrelated to design patterns
- Break the existing folder structure
- π Refactoring Guru β Design Patterns β Visual, easy-to-follow explanations
- π Gang of Four Book β The original reference (Gamma et al.)
- π Martin Fowler's Patterns β Enterprise-level pattern thinking
- SOLID Principles β The foundation every pattern builds on
- Clean Architecture β How patterns fit into larger system design
- Microservices Patterns β Cloud-native extensions of GoF thinking
This project is licensed under the MIT License β see LICENSE for details.
Free to use, modify, and distribute with attribution.
Mohamed Sakr β @devmohamedsakr-prog
- π Found a bug? Open an issue
- π¬ Have a question? Start a discussion
β If this repository helped you, please give it a star β it helps others find it!
Made with β€οΈ for developers who love clean code and thoughtful design.
Happy Learning! π