| name | design-patterns |
| description | Explains and applies the 23 Gang of Four (GoF) design patterns with idiomatic code examples in Python, C, C++, Java, C#, JavaScript, Rust, Go, PHP, and Swift. Use when users ask about design patterns (singleton, factory, builder, observer, strategy, decorator, adapter, etc.), when they need help choosing a pattern for a design problem, when refactoring code toward a pattern, or when they want a pattern example in a specific language. |
Design Patterns
Ground design-pattern answers in the reference files bundled with this skill instead of improvising definitions. Each pattern has a reference file explaining it and a folder of runnable examples in popular languages.
When to Use
- The user names a pattern directly ("show me the builder pattern in Rust")
- The user describes a design problem that maps to a pattern ("I need to create objects without specifying their concrete class")
- The user asks to compare patterns ("strategy vs state?")
- The user wants existing code refactored toward a pattern
Workflow
- Identify the pattern(s) from the user's question using the catalog below.
- Read the reference file at
references/<pattern>.md for the explanation, applicability, trade-offs, and related patterns.
- Read the language example at
examples/<pattern>/<pattern>.<ext> when the user works in (or asks for) a specific language. Adapt it to their domain rather than pasting the generic example verbatim.
- Answer with: what the pattern does, when to use it (and when not to), and code adapted to the user's context.
If the user's problem does not match any pattern cleanly, say so — do not force a pattern where a plain function or data structure is simpler.
Pattern Catalog
Creational
Patterns that abstract the instantiation process.
Structural
Patterns that compose classes and objects into larger structures.
Behavioral
Patterns that assign responsibilities and manage communication between objects.
Examples
Each pattern has a folder examples/<pattern>/ with self-contained, runnable examples named <pattern>.<ext>:
| Language | File | Notes |
|---|
| Python | <pattern>.py | |
| C | <pattern>.c | Emulated with structs and function pointers where the pattern relies on OOP |
| C++ | <pattern>.cpp | |
| Java | <pattern>.java | |
| C# | <pattern>.cs | |
| JavaScript | <pattern>.js | |
| Rust | <pattern>.rs | Uses traits and enums; no inheritance-based variants |
| Go | <pattern>.go | Uses interfaces and composition; no inheritance-based variants |
| PHP | <pattern>.php | |
| Swift | <pattern>.swift | |
Design patterns are expressed idiomatically per language — not as literal Java-style class hierarchies. C emulates with structs and function pointers; Go and Rust use interfaces, traits, and composition instead of inheritance. That is why those languages appear here but are omitted from the object-oriented-programming-paradigm skill, which requires first-class classes.
A language file may be absent when a pattern does not translate meaningfully to that language or is already built in (iterators in Python, module-scope singletons in JavaScript, sync.Once idioms in Go). The pattern's reference file notes such gaps and the idiomatic alternative.
Answering Guidelines
- Prefer the language's idiomatic form over a literal GoF class diagram (e.g. closures for Strategy in JavaScript, enums for State in Rust,
sync.Once for Singleton in Go).
- Mention trade-offs and simpler alternatives, not just the mechanics.
- When comparing patterns, read both reference files and contrast intent, not structure.
- Do not recommend Singleton, or any pattern, as a default; patterns are solutions to specific forces, not style goals.