Design Patterns (GoF) (50 Qs)•Part 2: Behavioral Patterns & Communication (11 - 24)
What is Clean Architecture (The Onion / Hexagonal Architecture)?
senior LevelCode ExampleSystem DesignBehavioral
In Simple Words: Introduced by Uncle Bob to create systems independent of frameworks, databases, and UI:
- Dependency Rule: Dependencies always point inwards toward the core domain!
- Entities (Core Domain): Pure business rules and models (Zero external dependencies).
- Use Cases (Application Business Rules): Orchestrates data flow to/from entities.
- Interface Adapters (Controllers, Gateways, Presenters): Translates data between use cases and external formats.
- Frameworks & Drivers (External): UI, Database (SQL/NoSQL), Web Server, Third-party APIs.
- The core business domain does not care whether data is stored in PostgreSQL, MongoDB, or an in-memory JSON file.
text
Clean Architecture Layers:
┌───────────────────────────────────────────────┐
│ Frameworks & Drivers (DB, UI, Devices) │
│ ┌───────────────────────────────────────┐ │
│ │ Interface Adapters (Controllers) │ │
│ │ ┌───────────────────────────────┐ │ │
│ │ │ Application Use Cases │ │ │
│ │ │ ┌───────────────────────┐ │ │ │
│ │ │ │ Entities (Domain) │ │ │ │
│ │ │ └───────────────────────┘ │ │ │
│ │ └───────────────────────────────┘ │ │
│ └───────────────────────────────────────┘ │
└───────────────────────────────────────────────┘
Dependency Arrow: External ───────► Inward (Domain)