Choosing Your Pattern
πΊοΈ NAVIGATION | Find the right pattern for your testing problem
This guide helps you choose the right pattern(s) based on your testing situation.
Quick Pattern Finder
What's your challenge? Find it in the table below:
| Your Challenge | Pattern Family | Pattern Name | Go To |
|---|---|---|---|
| How do I structure a test? | Testing | Pattern 1: AAA Pattern | Learn |
| How do I test error cases? | Testing | Pattern 2: Error Path Testing | Learn |
| How do I test edge cases? | Testing | Pattern 3: Boundary Conditions | Learn |
| How do I clean up resources? | Testing | Pattern 4: Resource Cleanup | Learn |
| Should I use mocks? | Testing | Pattern 5: Real Collaborators | Learn |
| How do I organize my code? | Architecture | Pattern 6: Generic Base Layer | Learn |
| How do I extend without duplicating? | Architecture | Pattern 7: Extension Layer | Learn |
| How do I avoid code duplication? | Architecture | Pattern 8: Composition Over Duplication | Learn |
| How do I avoid data inconsistency? | Architecture | Pattern 9: Single Source of Truth | Learn |
| How do I organize large modules? | Architecture | Pattern 10: Capability Grouping | Learn |
| How do I optimize for performance? | Design | Pattern 11: Zero-Cost Abstractions | Learn |
| How do I prevent type errors? | Design | Pattern 12: Type Safety with GATs | Learn |
| How do I prevent API misuse? | Design | Pattern 13: Sealed Traits | Learn |
| How do I validate at compile-time? | Design | Pattern 14: Compile-Time Validation | Learn |
| How do I enforce state machines? | Design | Pattern 15: Type State Enforcement | Learn |
| How do I manage fixture lifecycle? | Design | Pattern 16: Fixture Lifecycle Management | Learn |
| How do I build test data easily? | Design | Pattern 17: Builder-Driven Test Data | Learn |
| How do I prevent timeouts? | Design | Pattern 18: Timeout Defense | Learn |
| How do I manage feature flags? | Design | Pattern 19: Feature Gate Slices | Learn |
| How do I enforce patterns with macros? | Design | Pattern 20: Macro Pattern Enforcement | Learn |
By Category
Testing Patterns: "How Do I Write Better Tests?"
These patterns solve fundamental testing problems:
| Pattern | Problem | Solution |
|---|---|---|
| Pattern 1: AAA | Tests are hard to read | Structure: Arrange, Act, Assert |
| Pattern 2: Error Paths | I don't test failures | Test both success and error cases |
| Pattern 3: Boundaries | I miss edge cases | Systematically test limits |
| Pattern 4: Resource Cleanup | Tests leak resources | Automatic fixture cleanup |
| Pattern 5: Real Collaborators | Mocks hide integration bugs | Test with real implementations |
When to use: All the time. These are foundational.
Learning Path: Testing Patterns Learning Sequence
Architecture Patterns: "How Do I Organize Code?"
These patterns solve structural problems:
| Pattern | Problem | Solution |
|---|---|---|
| Pattern 6: Generic Base | Code is duplicate | Extract generic abstractions |
| Pattern 7: Extension Layer | I can't extend without modifying | Add layers for extensions |
| Pattern 8: Composition | DRY violations everywhere | Compose instead of duplicating |
| Pattern 9: Single Source | Data gets out of sync | One canonical source of truth |
| Pattern 10: Capability Groups | Module is too large | Organize by capability, not type |
When to use: During architecture phase and refactoring.
Learning Path: Architecture Patterns Learning Sequence
Design Patterns: "How Do I Make Code Safer?"
These patterns solve design and safety problems:
| Pattern | Problem | Solution |
|---|---|---|
| Pattern 11: Zero-Cost | Abstractions are slow | Zero-cost abstractions via generics |
| Pattern 12: Type Safety | Type errors at runtime | Use GATs for safety |
| Pattern 13: Sealed Traits | API is too easy to misuse | Seal traits to prevent misuse |
| Pattern 14: Compile-Time | Errors caught at runtime | Validate at compile-time |
| Pattern 15: Type State | State machines are error-prone | Encode states in types |
| Pattern 16: Fixture Lifecycle | Test setup is complex | Manage lifecycle with traits |
| Pattern 17: Builder Test Data | Building test data is tedious | Fluent builders for test data |
| Pattern 18: Timeout Defense | Tests hang forever | Timeout defense in depth |
| Pattern 19: Feature Gates | Feature flags are unreliable | Gate slices across codebase |
| Pattern 20: Macro Enforcement | Patterns are easy to violate | Use macros to enforce patterns |
When to use: During design and implementation.
Learning Path: Design Patterns Learning Sequence
Decision Trees
"I'm writing a test. Which pattern do I need?"
ββ Start: Writing a test
β
ββ What am I testing?
β ββ Normal behavior βββ Pattern 1: AAA Pattern
β ββ Error behavior βββ Pattern 2: Error Path Testing
β ββ Edge cases βββ Pattern 3: Boundary Conditions
β ββ Setup/teardown βββ Pattern 4: Resource Cleanup
β
ββ What should I test against?
β ββ Mock/fake βββ Consider Pattern 5: Real Collaborators
β ββ Real implementation βββ Pattern 5: Real Collaborators β
β
ββ How do I build test data?
ββ Complex data βββ Pattern 17: Builder-Driven Test Data
"I'm designing an architecture. Which patterns apply?"
ββ Start: Designing architecture
β
ββ How do I organize modules?
β ββ By type (models, handlers, etc.) βββ Consider Pattern 10: Capability Groups
β ββ By capability βββ Pattern 10: Capability Groups β
β
ββ How do I reuse code?
β ββ Copy-paste βββ NO! Use Pattern 8: Composition Over Duplication
β ββ Abstract base βββ Pattern 6: Generic Base Layer
β
ββ How do I extend without modifying?
β ββ Pattern 7: Extension Layer
β
ββ Where is the source of truth?
ββ Pattern 9: Single Source of Truth
"I'm designing APIs. Which patterns keep them safe?"
ββ Start: Designing public API
β
ββ Can downstream code misuse my API?
β ββ YES βββ Pattern 13: Sealed Traits
β
ββ Should errors be compile-time or runtime?
β ββ Compile-time βββ Pattern 14: Compile-Time Validation
β ββ Runtime βββ Less safe, but sometimes necessary
β
ββ Does state machine matter?
β ββ YES (auth states, connection states) βββ Pattern 15: Type State Enforcement
β ββ NO βββ Continue
β
ββ Are lifetimes complex?
β ββ YES βββ Pattern 12: Type Safety with GATs
β
ββ Should this be in macros?
ββ Pattern 20: Macro Pattern Enforcement
Learning by Difficulty
Beginner (Start Here)
- Pattern 1: AAA Pattern - Foundation
- Pattern 2: Error Path Testing - See what not to do
- Pattern 3: Boundary Conditions - Edge cases matter
- Pattern 4: Resource Cleanup - Don't leak resources
- Pattern 5: Real Collaborators - Test with real code
Time: ~3 hours | Content: Testing fundamentals
Intermediate (Build on Basics)
- Pattern 6: Generic Base Layer - Code organization
- Pattern 8: Composition Over Duplication - DRY principle
- Pattern 10: Capability Grouping - Module organization
- Pattern 17: Builder-Driven Test Data - Practical testing
- Pattern 14: Compile-Time Validation - Type safety
Time: ~4 hours | Content: Architecture and safety
Advanced (Master the Craft)
- Pattern 11: Zero-Cost Abstractions - Performance
- Pattern 12: Type Safety with GATs - Advanced types
- Pattern 15: Type State Enforcement - State machines
- Pattern 13: Sealed Traits - API design
- Pattern 18: Timeout Defense - Robustness
Time: ~5 hours | Content: Advanced design and optimization
Pattern Combination Guide
"I want to write production-quality tests"
Use these patterns together:
- Pattern 1: AAA - Structure your tests
- Pattern 2: Error Paths - Test failures
- Pattern 3: Boundaries - Test edge cases
- Pattern 4: Resource Cleanup - Clean automatically
- Pattern 5: Real Collaborators - Use real dependencies
- Pattern 17: Builder Test Data - Build complex test data
Expected outcome: Comprehensive, maintainable test suite
"I want to build a safe, extensible API"
Use these patterns together:
- Pattern 6: Generic Base - Reusable abstractions
- Pattern 7: Extension Layer - Allow extensibility
- Pattern 13: Sealed Traits - Prevent misuse
- Pattern 14: Compile-Time Validation - Validate early
- Pattern 15: Type State - Enforce state machines
- Pattern 20: Macro Enforcement - Enforce usage patterns
Expected outcome: Safe, extensible, hard-to-misuse API
"I want maximum performance"
Use these patterns together:
- Pattern 11: Zero-Cost Abstractions - Generic dispatch
- Pattern 12: Type Safety with GATs - Type-safe lifetimes
- Pattern 14: Compile-Time Validation - Zero runtime checks
- Pattern 8: Composition Over Duplication - Avoid copies
Expected outcome: Fast code with safety guarantees
FAQ
Q: How many patterns should I learn? A: Start with Testing Patterns (5), then Architecture (5) for production code. Advanced designers learn all 20.
Q: Do I need to learn them in order? A: No, but the beginner patterns are prerequisites for understanding advanced ones.
Q: Can I use just one pattern? A: Yes, but patterns work together. Combine related patterns for best results.
Q: Where do I go from here? A: Choose a Learning Sequence or pick a pattern you need right now.
Q: How do patterns relate to the application guide? A: Application guide shows how to apply patterns in practice. Cookbook explains why patterns exist. Use both together.
Next Steps
Choose your learning path:
- Testing Patterns Learning Path (90 minutes)
- Architecture Patterns Learning Path (60 minutes)
- Design Patterns Learning Path (120 minutes)
- All Patterns Quick Reference (Lookup table for all 20)
Or jump directly to the pattern you need from the Quick Finder above.
Remember: Patterns work together. As you learn each one, you'll recognize them appearing in others. That's the power of a pattern language.