Decision framework for architectural patterns including DI, SOA, Repository, Domain Events, Circuit Breaker, and Anti-Corruption Layer...
Architectural patterns solve specific structural problems. This skill provides a decision framework for when to apply each pattern, not a catalog to memorize.
Core philosophy: Patterns solve problems. No problem? No pattern needed.
Activate when:
These patterns provide the structural foundation for maintainable systems. Apply unless you have specific reasons not to.
| Pattern | Problem Solved | Signal to Apply |
|---|---|---|
| Dependency Injection | Tight coupling, untestable code | Classes instantiate their own dependencies |
| Service-Oriented Architecture | Monolithic tangles, unclear boundaries | Business logic scattered, no clear ownership |
DI quick example ā before and after:
# BEFORE: tight coupling, hard to test
class OrderService:
def __init__(self):
self.db = PostgresDatabase() # concrete dependency
self.mailer = SmtpMailer() # concrete dependency
# AFTER: dependencies injected, easily testable
class OrderService:
def __init__(self, db: Database, mailer: Mailer):
self.db = db
self.mailer = mailer
# In tests: OrderService(db=FakeDatabase(), mailer=FakeMailer())
These patterns address specific problems. Don't apply preemptively.
| Pattern | Problem Solved | Signal to Apply |
|---|---|---|
| Repository | Data access coupling | Services know about database details |
| Domain Events | Circular dependencies, temporal coupling | Service A calls B calls C calls A |
| Anti-Corruption Layer | External system coupling | External API changes break your code |
| Circuit Breaker | Cascading failures | One slow service takes down everything |
ā Foundational Patterns Detail ā Situational Patterns Detail
Is code hard to test?
āā Yes ā Apply Dependency Injection
āā No ā Continue
Is business logic scattered?
āā Yes ā Apply Service-Oriented Architecture
āā No ā Continue
Do services know database details?
āā Yes ā Apply Repository Pattern
āā No ā Continue
Do services call each other in cycles?
āā Yes ā Apply Domain Events
āā No ā Continue
Does external API change break your code?
āā Yes ā Apply Anti-Corruption Layer
āā No ā Continue
Does one slow service break everything?
āā Yes ā Apply Circuit Breaker
āā No ā Current patterns sufficient
When starting a new system:
When refactoring existing system:
_multi quality indicators (https://doc.casthighlight.com/)STOP when:
ALL of these mean: STOP. Identify the specific problem first.
Common effective combinations:
| Scenario | Patterns |
|---|---|
| New microservice | DI + SOA + Repository |
| External API integration | DI + ACL + Circuit Breaker |
| Event-driven system | DI + SOA + Domain Events |
| Data-heavy application | DI + SOA + Repository + Unit of Work |
Remember: Patterns exist to solve problems. Start with the problem, not the pattern.