Course contentsShow
← Software Engineering
Lesson 1264 of 1,93338. Layered and Modular ArchitectureFree lesson

The Ports and Adapters Pattern

Learn how Hexagonal Architecture separates core business logic from external concerns using ports and adapters.

The Ports and Adapters Pattern

You'll learn: How the Ports and Adapters pattern (Hexagonal Architecture) isolates your core business logic from external systems using ports and adapters.

The Core Idea

The Ports and Adapters pattern, also known as Hexagonal Architecture, organizes your application around a central core of business logic that remains completely independent of external concerns like databases, web frameworks, or message queues.

Think of your application as a medieval castle. The castle's inner keep (your business logic) has ports—defined entry and exit points like gates and windows. Adapters are like specialized bridges or mechanisms that connect those ports to the outside world. Whether visitors arrive by horse, boat, or carriage doesn't matter to the castle itself—the adapters handle the translation.

Ports: The Contracts

A port is an interface that defines how your core application interacts with the outside world. There are two types:

  • Inbound (driving) ports: Interfaces that let external systems call your business logic (e.g., "handle user registration")
  • Outbound (driven) ports: Interfaces your business logic uses to request external services (e.g., "save user to storage")

Adapters: The Implementations

An adapter is a concrete implementation that connects a port to a specific technology. You might have:

  • A REST API adapter that receives HTTP requests and calls an inbound port
  • A PostgreSQL adapter that implements an outbound port for data persistence
  • A file system adapter that could swap in during testing

Why It Matters

This separation means your core business rules don't import database libraries or web frameworks. They stay pure, testable, and technology-agnostic. You can swap a MySQL adapter for PostgreSQL, or test with an in-memory adapter without touching business logic.

Key Takeaway: Ports and Adapters keeps your business logic isolated in the center, surrounded by interchangeable adapters that connect it to the messy real world through well-defined port interfaces.