A comprehensive guide to design patterns in C#, inspired by the Gang of Four (GoF) book. This repository contains documentation and minimalistic code samples demonstrating each design pattern.
The patterns are organized into three main categories:
Patterns that deal with object creation mechanisms.
- Singleton - Ensures a class has only one instance
- Factory Method - Creates objects without specifying exact classes
- Abstract Factory - Creates families of related objects
- Builder - Constructs complex objects step by step
- Prototype - Creates new objects by copying existing ones
Patterns that deal with object composition and relationships.
- Adapter - Allows incompatible interfaces to work together
- Bridge - Separates abstraction from implementation
- Composite - Composes objects into tree structures
- Decorator - Adds behavior to objects dynamically
- Facade - Provides a simplified interface to a complex system
- Flyweight - Shares common state between multiple objects
- Proxy - Provides a surrogate or placeholder for another object
Patterns that deal with object collaboration and responsibility.
- Chain of Responsibility - Passes requests along a chain of handlers
- Command - Encapsulates a request as an object
- Iterator - Accesses elements of a collection sequentially
- Mediator - Defines how objects interact
- Memento - Captures and restores an object's state
- Observer - Notifies multiple objects about state changes
- State - Alters object behavior when its state changes
- Strategy - Defines a family of interchangeable algorithms
- Template Method - Defines the skeleton of an algorithm
- Visitor - Separates algorithms from objects they operate on
Each pattern directory contains:
README.md- Documentation explaining the pattern- A minimalistic C# application demonstrating the pattern
These samples are built with:
- .NET 10 - The latest LTS release with enhanced runtime performance
- C# 14 - Featuring new language improvements including:
fieldkeyword for cleaner property implementations- Null-conditional assignment (
?.=) - Extension members and improved
Span<T>support - Enhanced lambda expressions
To run the samples, install the .NET 10 SDK.
Feel free to contribute by adding more examples or improving existing documentation.