Selects and implements a C# design pattern only when it solves a stated variation, construction, or integration problem. Use for Strategy, Factory, Decorator, Adapter, and composition choices; do not force patterns where a simple function is clearer (use csharp-best-practices).
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Design Patterns?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/k4ndev-design-patterns)
---
name: design-patterns
description: Selects and implements a C# design pattern only when it solves a stated variation, construction, or integration problem. Use for Strategy, Factory, Decorator, Adapter, and composition choices; do not force patterns where a simple function is clearer (use csharp-best-practices).
---
# Design Patterns
## Purpose
Choose the smallest design mechanism that makes a real variation or integration boundary explicit without adding unnecessary machinery.
## Scope
Strategy, Factory, Decorator, Adapter, composition, and appropriate built-in .NET/ASP.NET extension points.
## When to use
- The user asks which pattern fits a concrete object creation, algorithm variation, or cross-cutting behavior problem.
- Existing conditional growth or vendor coupling makes the design difficult to change.
## Do not use for
- Pattern catalog/trivia without a project context: explain briefly without creating architecture.
- Project dependency direction: use `clean-architecture`; responsibility design: use `solid-principles`.
## Required context
Know the variation points, callers, object lifetimes, failure behavior, test seams, framework features, and expected number of implementations.
## Workflow
1. Describe the problem without pattern terminology.
2. Compare a function/switch/composition with the requested pattern.
3. Pick Strategy for interchangeable behavior, Factory for genuinely variable/complex construction, Decorator for wrapping a stable contract, Adapter for an external/legacy shape.
4. Check built-in DI, middleware, `DelegatingHandler`, Options, and hosted-service facilities before custom frameworks.
5. Show a focused implementation and explain lifetime/registration; test behavior, selection, and failure paths.
## Decision rules
- Prefer composition for independently varying behavior; do not create a plugin architecture for a fixed pair of cases.
- Use DI to compose known services, not as hidden service location inside domain logic.
- A pattern is justified only if it reduces coupling or clarifies behavior in this scenario.
## Expected output format
State the problem, selected mechanism, why simpler alternatives fail or are sufficient, sample C# code, lifetime considerations, and tests.
## Common mistakes
- Naming a pattern before understanding the problem, over-generalizing factories, wrapping every class in a decorator, or turning a switch into needless polymorphism.
## Edge cases
- A Strategy selected by configuration needs validation and a safe unknown-key path.
- Decorators must preserve cancellation, exceptions, disposal, and result semantics.
- A factory-created `IDisposable` object's ownership must be explicit.
## Examples
See [pattern examples](examples.md) for a payment strategy and a simpler fixed-case alternative.
## Test scenarios
- Positive: “Add two tax calculation policies selected by jurisdiction without growing a giant checkout conditional.”
- Negative: “My process deadlocks under load” routes to `async-concurrency` / `dotnet-diagnostics`.
- Boundary: “Add retry/circuit breaker to `HttpClient`” primarily uses `aspnetcore-patterns` (and resilience guidance if present), not a custom Decorator by default.
## Related skills
`solid-principles`, `clean-architecture`, `aspnetcore-patterns`, `csharp-best-practices`, `testing-quality`.