A comprehensive collection of software design patterns implemented in C#, Java, and JavaScript with practical, easy-to-understand examples.
This repository contains implementations of all 23 Gang of Four (GoF) design patterns across three popular programming languages. Each pattern includes:
- Clear, well-commented code examples
- Practical use cases
- Implementation in C#, Java, and JavaScript
- Easy-to-follow structure
Whether you're preparing for interviews, working with legacy code, or improving your software architecture skills, this repository serves as a practical reference guide.
Design patterns are reusable solutions to common problems in software design. They represent best practices developed over time by experienced software engineers. The Gang of Four (GoF) patterns were documented in the influential 1994 book "Design Patterns: Elements of Reusable Object-Oriented Software" by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides.
Creational patterns deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.
- Factory Method - Defines an interface for creating objects but delegates instantiation to subclasses
- Abstract Factory - Produces families of related objects without specifying their concrete classes
- Builder - Provides a step-by-step approach to construct complex objects
- Prototype - Creates new objects by copying existing ones instead of constructing from scratch
- Singleton - Ensures only one instance of a class exists and provides global access to it
Structural patterns deal with object composition and typically identify simple ways to realize relationships between different objects.
- Adapter - Allows incompatible interfaces to work together by wrapping an interface around an existing class
- Bridge - Decouples an abstraction from its implementation so they can vary independently
- Composite - Composes objects into tree structures to represent part-whole hierarchies
- Decorator - Dynamically adds new behavior to objects without modifying their structure
- Facade - Provides a simplified interface to a larger and more complex system
- Flyweight - Shares common object instances to minimize memory usage
- Proxy - Acts as a placeholder to control access to an object
Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects.
- Chain of Responsibility - Passes requests along a chain of handlers until one handles it
- Command - Encapsulates a request as an object, allowing undo and queuing mechanisms
- Interpreter - Defines a representation for grammar along with an interpreter
- Iterator - Provides a way to traverse a collection without exposing its internal details
- Mediator - Reduces direct dependencies between objects by centralizing communication
- Memento - Captures an object's state for later restoration
- Observer - Establishes a dependency between objects for change notifications
- State - Allows an object to alter its behavior when its internal state changes
- Strategy - Defines a family of algorithms, encapsulates each one, and makes them interchangeable
- Template Method - Defines the skeleton of an algorithm, allowing subclasses to provide concrete behavior
- Visitor - Separates an algorithm from an object structure
DesignPatterns/
├── CSharp/
│ ├── Creational/
│ │ ├── FactoryMethod/
│ │ ├── AbstractFactory/
│ │ ├── Builder/
│ │ ├── Prototype/
│ │ └── Singleton/
│ ├── Structural/
│ │ ├── Adapter/
│ │ ├── Bridge/
│ │ ├── Composite/
│ │ ├── Decorator/
│ │ ├── Facade/
│ │ ├── Flyweight/
│ │ └── Proxy/
│ └── Behavioral/
│ ├── ChainOfResponsibility/
│ ├── Command/
│ ├── Interpreter/
│ ├── Iterator/
│ ├── Mediator/
│ ├── Memento/
│ ├── Observer/
│ ├── State/
│ ├── Strategy/
│ ├── TemplateMethod/
│ └── Visitor/
├── Java/
│ └── (same structure as CSharp)
└── Javascript/
└── (same structure as CSharp)
- C#: .NET SDK 6.0 or later
- Java: JDK 11 or later
- JavaScript: Node.js 14 or later
Navigate to any pattern directory in your language of choice and follow the instructions in the pattern's README file.
C# Example:
cd CSharp/Creational/Singleton
dotnet run
Java Example:
cd Java/Creational/Singleton
javac *.java
java Main
JavaScript Example:
cd Javascript/Creational/Singleton
node index.js
If you're new to design patterns, we recommend studying them in this order:
- Start with Creational patterns - Understanding object creation is fundamental
- Move to Structural patterns - Learn how to compose objects effectively
- Finish with Behavioral patterns - Master object interaction and responsibility
- Interview Preparation: Master the patterns commonly asked in technical interviews
- Code Review: Identify and apply appropriate patterns in your codebase
- Architecture Design: Make informed decisions about software structure
- Legacy Code: Understand and refactor existing codebases
- Team Communication: Use pattern names as a shared vocabulary
Contributions are welcome! If you'd like to:
- Add implementations in additional languages
- Improve existing examples
- Fix bugs or typos
- Add more detailed explanations
Please feel free to submit a pull request.
- Design Patterns: Elements of Reusable Object-Oriented Software - The original Gang of Four book
- Refactoring.Guru - Design Patterns - Excellent visual explanations
- SourceMaking - Design Patterns - Comprehensive pattern catalog
This project is licensed under the MIT License - see the LICENSE file for details.
- The Gang of Four (Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides) for documenting these timeless patterns
- The open-source community for continuous learning and knowledge sharing
Found this helpful? Give it a ⭐️ to show your support!