Java Omnibus
OOAD & Architecture / Architectural Styles / Hexagonal vs Onion vs Clean Architecture
Hexagonal Onion Clean Architecture Comparison
Architectural Styles

Hexagonal, onion, and clean architecture overlap heavily, but they emphasize different teaching lenses.

These three styles are often treated as synonyms. In practice, they share a strong family resemblance: protect the core, keep dependencies pointing inward, and isolate infrastructure. The differences are mostly about emphasis and teaching model, but that difference can still matter when teams are learning or reviewing architecture.

Why This Topic Matters

Why it matters in real Java systems

Teams waste time arguing about labels when they should be discussing dependency direction, port design, domain richness, and adapter placement. This comparison helps move that conversation back to substance.

Core Explanation

The main architectural idea

Hexagonal architecture emphasizes ports and adapters. Onion architecture emphasizes concentric dependency direction with the domain in the center. Clean architecture emphasizes the dependency rule and the separation between entities, use cases, interface adapters, and frameworks. The overlap is strong because all three try to keep business policy from depending on infrastructure.

The most helpful question is usually not which term is most correct. It is which teaching lens will help your team apply the boundary rules consistently in the codebase you actually have.

Concept Breakdown

Key ideas to keep in mind

Concept

Hexagonal

Best when the team benefits from explicit port and adapter vocabulary around integrations and use-case entry points.

Concept

Onion

Best when the domain-centered visual model helps the team reason clearly about inward dependencies.

Concept

Clean

Best when the dependency rule and use-case orientation are the strongest teaching anchors for the team.

Comparison table

Comparison table

Style Strongest teaching emphasis Typical Java payoff
HexagonalPorts and adaptersClear integration seams and replaceable infrastructure
OnionDomain at the centerStronger domain-centric thinking and inward dependency intuition
CleanDependency rule and use casesExplicit business policy separation from frameworks
Java Example

Java example in the Order Management domain

Java example: one use case viewed through three lenses
package org.javaomnibus.ecommerce.application;

public final class CapturePaymentUseCase {
    private final PaymentPort paymentPort;
    private final OrderStore orderStore;

    public CapturePaymentUseCase(PaymentPort paymentPort, OrderStore orderStore) {
        this.paymentPort = paymentPort;
        this.orderStore = orderStore;
    }
}

// Hexagonal lens: PaymentPort and OrderStore are output ports.
// Onion lens: the use case sits outside the domain center but still points inward.
// Clean lens: the use case stays independent from controllers, frameworks, and gateways.
Code Walkthrough

Step by step

  1. The same code can often be explained through all three styles.
  2. The code becomes hexagonal when you stress ports and adapters.
  3. It becomes onion-like when you stress the domain center and dependency rings.
  4. It becomes clean when you stress the dependency rule and use-case boundary.
Real-World Usage

Where this helps in practice

  • Resolving team confusion around architecture terminology
  • Choosing a teaching model for a modular monolith or service core
  • Reviewing whether the code actually follows inward dependency rules
Trade-Offs

Trade-offs and when not to overuse it

  • The differences are useful pedagogically, but overemphasizing them can distract from the shared architectural discipline that really matters.
  • Teams should pick the vocabulary that helps them enforce boundaries best.
Common Mistakes

Frequent mistakes to watch for

  • Spending more time on labels than on dependency leaks
  • Declaring one of these styles while still importing framework code into the core
  • Assuming the styles are identical in emphasis and therefore never clarifying terms for the team
  • Using architecture vocabulary without code review discipline to back it up
Related Pages

Related concepts


What To Read Next