Quick revision
The short answer
Clean architecture arranges code in concentric rings: entities, use cases, interface adapters, and frameworks and drivers, and its dependency rule says source code dependencies point only inward, so inner rings know nothing about outer ones and data crossing a boundary uses simple structures defined by the inner ring. Hexagonal, onion and clean architecture share this core idea with different names and emphasis. Applied literally, clean architecture can add many classes per use case, so we adapt it to the system's size.
What to remember
In this answer, we saw that clean architecture arranges code in rings of entities, use cases, interface adapters, and frameworks and drivers, with the dependency rule that source code dependencies point only inward. Inner rings define interfaces and simple data structures for crossing boundaries, and outer rings implement and adapt to them. Hexagonal and onion architecture express the same idea with different pictures and emphasis, and in practice we keep the rule while trimming the class count to fit the size of the system.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Clean architecture arranges code in concentric rings: entities, use cases, interface adapters, and frameworks and drivers, and its dependency rule says source code dependencies point only inward, so inner rings know nothing about outer ones and data crossing a boundary uses simple structures defined by the inner ring. Hexagonal, onion and clean architecture share this core idea with different names and emphasis. Applied literally, clean architecture can add many classes per use case, so we adapt it to the system's size.
Why this question is asked
Clean architecture is one of those terms that appears in job descriptions, code review comments and conference talks, often meaning slightly different things. Interviewers ask about it, and how it relates to hexagonal and onion architecture, to see whether we understand the shared principle beneath the names and whether we apply it with judgement rather than as a template. In this answer, we will walk through the four rings, explain the dependency rule and how it is implemented in Java, look at how data crosses boundaries, compare the three styles side by side, and discuss how far to take it in a real Spring service.
1The Four Rings
Robert C. Martin described clean architecture in 2012 as a set of concentric circles. At the centre are entities: the enterprise-wide business objects and rules, such as an Order that knows it cannot be cancelled after shipping. Around them are use cases: application-specific business actions, such as PlaceOrder or CancelOrder, which orchestrate entities to achieve one goal.
The third ring is interface adapters: code that converts data between the form the use cases want and the form external things want. Controllers, presenters, and gateway implementations that talk to the database live here. The outermost ring is frameworks and drivers: Spring, the web server, the database, the message broker, the UI. Martin's point is that these are details, and the business should not depend on details.
- Frameworks and driversSpring Boot, Tomcat, PostgreSQL, Kafka
- Interface adapterscontrollers, presenters, repository implementations
- Use cases
PlaceOrder,CancelOrder; application rules - EntitiesOrder, Money; enterprise business rules
4 layers, from the top down. Step through them one at a time.
Read top to bottom as outside to inside. Every source dependency points down this stack, never up.
2The Dependency Rule
The rule that makes the rings work is simple: source code dependencies point only inward. Nothing in an inner ring can mention anything in an outer ring by name: no class, no function, no annotation, no data format. An entity does not know a use case exists; a use case does not know whether it is called from a REST controller or a test, or whether data is stored in PostgreSQL.
But use cases do need to save and load data, which lives in an outer ring. The trick is dependency inversion: the use case ring defines an interface for what it needs, and the outer ring implements it. At runtime, control flows outward from the use case to the database, while source code dependency points inward from the database code to the interface.
Notice that CancelOrder imports only inner-ring types. JpaOrderGateway imports both JPA and the use case interface, so it depends inward.
3How Data Crosses the Boundaries
When data passes from one ring to another, it travels in simple structures that the inner ring defines. In the example, CancelOrderRequest and CancelOrderResponse are plain records owned by the use case ring. The controller builds a request from the HTTP body and turns the response into JSON; the use case never sees the HTTP body or the JSON.
The reason is that passing an outer-ring object inward would create a dependency on it. If the use case accepted an HttpServletRequest or returned a JPA entity, it would depend on the servlet API or on JPA. So the boundary is defined by the inner ring, and the outer ring adapts to it.
4Clean, Hexagonal and Onion Compared
Hexagonal architecture, from Alistair Cockburn in 2005, describes the same idea as an application core surrounded by ports and adapters, emphasising that the application can be driven by many actors and can drive many external systems. Onion architecture, named by Jeffrey Palermo in 2008, uses concentric layers of domain model, domain services and application services, with infrastructure outside, emphasising the domain model at the centre. Clean architecture, from 2012, combines these with explicit rings for entities and use cases and a single stated dependency rule.
All three share the same core: business logic in the middle, frameworks and infrastructure outside, and dependencies pointing inward through interfaces the inner code owns. The differences are mostly in naming and in how finely the centre is divided. In practice, a well-built hexagonal Spring service and a well-built clean architecture Spring service look nearly the same.
| Aspect | Hexagonal | Onion | Clean |
|---|---|---|---|
| Introduced | Cockburn, 2005 | Palermo, 2008 | Martin, 2012 |
| Picture | core with ports on its edges | concentric layers | concentric rings |
| Centre | application core | domain model | entities, then use cases |
| Emphasis | many drivers and driven systems | domain model first | one explicit dependency rule |
| Boundary mechanism | ports and adapters | interfaces in inner layers | interfaces and boundary data structures |
5Adapting It to the System's Size
Applied literally, clean architecture can produce many classes for each use case: a request model, a response model, an input boundary interface, an output boundary or presenter interface, the interactor itself, a controller, a presenter implementation, a gateway interface and its implementation, plus mappers. For one endpoint that is ten classes. In a complex domain with long-lived rules this structure pays off; in a service with twenty CRUD endpoints it turns into hundreds of classes that mostly copy fields.
So experienced teams keep the rule and trim the ceremony. Common simplifications are skipping presenter interfaces and letting use cases return a result record, skipping input boundary interfaces and letting the controller call the use case class directly, and using records for request and response models. The non-negotiable part is the dependency direction; the number of classes should follow the complexity of the domain.
6Follow-up questions
Is the presenter really necessary in a REST service?
Usually not. In Martin's original description, the use case calls an output boundary that a presenter implements, which is useful for UIs with complex view models. In a REST service, returning a simple result record from the use case and letting the controller map it to a response keeps the dependency rule intact with fewer classes.
Can entities be JPA entities?
Under a strict reading, no, because jakarta.persistence annotations are a framework detail in the innermost ring. Many teams accept annotated entities as a pragmatic compromise for simpler services. For complex, long-lived domains, separate persistence entities in the adapter ring keep the centre clean.
How do you enforce the dependency rule in Spring?
With build modules, so the inner modules do not have Spring or JPA on their compile classpath, or with ArchUnit rules such as onionArchitecture(), which checks that domain, application and adapter packages only depend inward.
7Conclusion
In this answer, we saw that clean architecture arranges code in rings of entities, use cases, interface adapters, and frameworks and drivers, with the dependency rule that source code dependencies point only inward. Inner rings define interfaces and simple data structures for crossing boundaries, and outer rings implement and adapt to them. Hexagonal and onion architecture express the same idea with different pictures and emphasis, and in practice we keep the rule while trimming the class count to fit the size of the system.
Open with
Clean architecture arranges the code in concentric rings. At the centre are entities, the core business objects and rules. Around them are use cases, the application-specific actions like cancel an order.
Then, if they want more
Then interface adapters, such as controllers and repository implementations, and on the outside frameworks and drivers: Spring, the web server, the database. The dependency rule says source code dependencies only point inward, so inner rings know nothing about outer ones.
When a use case needs to save data, it defines an interface in its own ring, and the JPA code in the adapter ring implements it, which is dependency inversion: control flows outward at runtime, but the code depends inward.
Data crossing a boundary uses simple structures owned by the inner ring, like a request and response record, so the use case never sees HTTP or JPA types. Hexagonal and onion architecture share exactly this idea.
Hexagonal talks about a core with ports and adapters, onion about layers around the domain model, and clean architecture splits the centre into entities and use cases and states the rule explicitly. The differences are mostly naming and emphasis.
Applied literally, clean architecture can mean ten classes per use case, so I keep the dependency direction strict and trim the rest, for example returning a result record instead of using presenter interfaces.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain clean architecture and its dependency rule. How does it differ from hexagonal...": https://scrivio-interview-library.netlify.app/java/application-architecture/e488b1de2c/