What is clean architecture?
Clean architecture is a way to organize code so that the business rules do not depend on the tools around them. Robert C. Martin described it as a set of concentric rings. The most stable and important code sits in the middle. The web framework, the database and other details sit on the outside, where they can be replaced without touching the core.
This template draws the rings and fills them with an ordering app. Its subtitle states the one constraint that makes the pattern work: source code may only point inward, so the business rules in the middle never know about the web, the database or the framework. The right side of the board explains each ring and shows what crossing from one ring to the next looks like in code.
The four rings
From the center outward, the rings are Entities, Use cases, Interface adapters and Frameworks and drivers. Each ring has small chips with example members. Entities holds Order, Money and Customer. Use cases holds PlaceOrder and CancelOrder. Interface adapters holds Controllers and Repositories. The outer ring holds the app's database, its payment SDK and its web and UI frameworks.
Three short arrows cut through the ring borders toward the center, next to the label “Dependencies point inward”. They are the most important marks in the drawing. An arrow from an outer ring to an inner one means the outer code may import the inner code. There is no arrow going the other way.
What belongs in each ring
Four rows beside the circle define the rings in a sentence each:
- Entities: business rules that would hold without any software. Think of an order's total or a refund limit.
- Use cases: what the app does, one class for each user goal. The row lists PlaceOrder, CancelOrder and RefundPayment.
- Interface adapters: they turn web requests and rows into use-case data. Controllers, presenters and repositories live here.
- Frameworks and drivers: tools and details, kept at the edge so they can be swapped.
The first definition is a useful test. If a rule would still be true in a shop run on paper, it is an entity. If it exists only because there is an app, it is a use case.
The dependency rule
Below the four rows, a highlighted box gives the rule in full: an inner ring never names anything in an outer ring. No SQL, HTTP or framework types reach the middle.
“Names” is the exact word. A use case may not import a database client, accept a web request object or return a framework's response type. If the word for an outer tool appears in an inner file, the rule is broken, even when the code works. That makes the rule easy to check in a code review, and some teams enforce it with a lint rule on imports.
Crossing a boundary
If the center cannot name the outside, how does a use case return a result to the screen? The small diagram at the bottom right answers that. It has five boxes in a row: Controller, an interface called Input port, Interactor, an interface called Output port, and Presenter.
The controller calls the input port, and the interactor implements it. The interactor calls the output port, and the presenter implements that. As the caption says, control flows left to right while each dependency points at a use-case interface. The use case owns both interfaces, so the adapters depend on it and never the reverse. This is dependency inversion, and it is the trick that keeps the arrows pointing inward.
How to use the clean architecture template
- Open the template and keep the rings. Only the chips and the example lines need to change.
- Fill the center first. Replace Order, Money and Customer with the nouns of your own business, the ones a domain expert would use.
- List your use cases as verbs. Write one chip per user goal, in the style of PlaceOrder and CancelOrder.
- Sort the rest outward. Put every controller, repository and gateway in the adapter ring, and every library or service in the outer ring.
- Hunt for arrows that point out. Take one real feature and trace its imports. Mark any inner file that names an outer tool, and plan an interface to fix it.
To draw the classes of one use case in detail, FlowDraw AI can generate a class or sequence diagram from a description.
When clean architecture fits, and other views to draw
The pattern pays off in applications with real business rules and a long life: the rules can be tested without a database, and a framework can be upgraded or replaced without rewriting them. For a small script or a simple data-entry app, four rings are more structure than the code needs.
This board shows how code is layered, not how the running system is deployed. For servers, databases and the calls between them, use the system architecture template. To work out the entities in the center and how they relate, draw a domain model. To show the modules and their interfaces in standard notation, a component diagram does the job.
Questions about the Clean architecture template
What is the dependency rule in clean architecture?
Source code dependencies may only point inward. Code in an inner ring cannot import, mention or know about anything in an outer ring. Outer code may use inner code freely. Everything else in the pattern follows from this.
How is clean architecture different from hexagonal architecture?
They share the same idea: business logic in the middle, tools at the edge, and interfaces in between. Hexagonal architecture speaks of ports and adapters around one core. Clean architecture splits that core into entities and use cases and draws it as rings.
Do I need exactly four rings?
No. Four is the usual picture, but a small service may merge entities and use cases, and a large one may add a ring. What may not change is the direction of the dependencies.
Where does the database go?
In the outer ring, as a detail. The use case defines a repository interface that says what it needs, such as saving an order. A class in the adapter ring implements that interface with real queries, so the use case never sees SQL.