A comprehensive implementation of the 23 Gang of Four (GoF) Design Patterns in C# with examples and unit tests.
- Overview
- Project Structure
- Patterns Implemented
- Getting Started
- Running Examples
- Running Tests
- Contributing
This repository contains complete implementations of all 23 classic Design Patterns from the "Gang of Four" book, organized into three categories:
- Creational Patterns: Deal with object creation mechanisms
- Structural Patterns: Deal with object composition
- Behavioral Patterns: Deal with object collaboration and responsibility distribution
DesignPatternsInCSharp/
??? README.md
??? .gitignore
??? DesignPatterns.sln
??? src/
? ??? CreationalPatterns/
? ??? StructuralPatterns/
? ??? BehavioralPatterns/
??? tests/
? ??? CreationalPatterns.Tests/
? ??? StructuralPatterns.Tests/
? ??? BehavioralPatterns.Tests/
??? examples/
? ??? ConsoleApp/
??? docs/
??? images/
??? patterns/
- ? Singleton: Ensures a class has only one instance
- ? Factory Method: Creates objects without specifying exact class
- ? Abstract Factory: Creates families of related objects
- ? Builder: Constructs complex objects step by step
- ? Prototype: Creates new objects by copying existing ones
- ? Adapter: Allows incompatible interfaces to work together
- ? Bridge: Separates abstraction from implementation
- ? Composite: Composes objects into tree structures
- ? Decorator: Adds responsibilities to objects dynamically
- ? Facade: Provides simplified interface to complex subsystem
- ? Flyweight: Shares data to support large numbers of objects
- ? Proxy: Provides placeholder for another object
- ? Chain of Responsibility: Passes requests along a chain of handlers
- ? Command: Encapsulates requests as objects
- ? Interpreter: Defines grammar and interpreter for a language
- ? Iterator: Accesses elements sequentially without exposing structure
- ? Mediator: Defines simplified communication between classes
- ? Memento: Captures and restores object's internal state
- ? Observer: Defines one-to-many dependency between objects
- ? State: Allows object to alter behavior when state changes
- ? Strategy: Defines family of algorithms and makes them interchangeable
- ? Template Method: Defines skeleton of algorithm in base class
- ? Visitor: Separates algorithms from objects they operate on
- .NET 8.0 SDK or later
- Visual Studio 2022, Visual Studio Code, or JetBrains Rider
# Clone the repository
git clone https://github.com/yourusername/DesignPatternsInCSharp.git
cd DesignPatternsInCSharp
# Restore dependencies
dotnet restore
# Build the solution
dotnet build
# Run all tests
dotnet test# Run the console application with examples
dotnet run --project examples/ConsoleApp/ConsoleApp.csproj# Run all tests
dotnet test
# Run tests for a specific category
dotnet test tests/CreationalPatterns.Tests/CreationalPatterns.Tests.csproj
dotnet test tests/StructuralPatterns.Tests/StructuralPatterns.Tests.csproj
dotnet test tests/BehavioralPatterns.Tests/BehavioralPatterns.Tests.csproj
# Run tests with coverage
dotnet test --collect:"XPlat Code Coverage"Detailed documentation for each pattern can be found in the docs/patterns/ directory:
- Each pattern includes:
- Intent and motivation
- UML diagram
- Real-world examples
- Implementation details
- Usage guidelines
- Advantages and disadvantages
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
- "Design Patterns: Elements of Reusable Object-Oriented Software" by Gang of Four
- Refactoring.Guru - Design Patterns
- Microsoft Docs - Design Patterns
If you find this repository helpful, please consider giving it a star!