Skip to content

Latest commit

Β 

History

46 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation


🎯 Design Patterns

Master the Art of Software Architecture


License: MIT Stars Forks Patterns


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



πŸ“– About

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.


πŸ—ΊοΈ Pattern Categories at a Glance

Category Focus Patterns
πŸ”§ Creational How objects are created 5
πŸ—οΈ Structural How objects are composed 7
πŸ“‘ Behavioral How objects communicate 11
Total GoF Patterns 23

πŸ”§ Creational Patterns

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

πŸ—οΈ Structural Patterns

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

πŸ“‘ Behavioral Patterns

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

πŸš€ Quick Start

Step 1 β€” Clone the Repository

git clone https://github.com/devmohamedsakr-prog/Design-Patterns.git
cd Design-Patterns

Step 2 β€” Navigate to a Pattern

# Example: explore the Singleton implementation
cd Creational/Singleton/CurrencyConverter/After

Step 3 β€” Build & Run

dotnet restore     # Restore NuGet packages
dotnet build       # Build the project
dotnet test        # Run the test suite
dotnet run         # Run the application

Step 4 β€” Study the Structure

Each 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

πŸ“ Full Repository Structure

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

πŸ’‘ Choosing the Right Pattern

Not sure which pattern to use? Use these decision points:

πŸ”§ Use a Creational Pattern if…

  • 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

πŸ—οΈ Use a Structural Pattern if…

  • 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

πŸ“‘ Use a Behavioral Pattern if…

  • 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

🀝 Contributing

Contributions are welcome β€” new implementations, improvements, and documentation all count.

How to Contribute

# 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-pattern

Guidelines

Do βœ…

  • Write clear, commented code with real-world context
  • Include a Before/ and After/ 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

πŸ“š Resources

Essential Reading

Related Concepts

  • 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

πŸ“„ License

This project is licensed under the MIT License β€” see LICENSE for details.
Free to use, modify, and distribute with attribution.


πŸ‘¨β€πŸ’» Author

Mohamed Sakr β€” @devmohamedsakr-prog


⭐ 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! πŸš€

About

A comprehensive, well-organized repository of software design patterns with clean implementation examples and detailed documentation.

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages