Skip to content

Commit 9de12ea

Browse files
committed
docs: update clean architecture
1 parent 4afabdb commit 9de12ea

19 files changed

+170
-86
lines changed

Diff for: clean-architecture/README.md

+58-44
Original file line numberDiff line numberDiff line change
@@ -2,54 +2,48 @@
22
title: "Clean Architecture - A Software Maintainable Architectural style."
33
shortTitle: Clean Architecture
44
description: "Learn the Clean Architecture Style in Java with real-world examples, code snippets, and class diagrams. Enhance your coding skills with our detailed explanations."
5-
category: Behavioral
5+
category: Architectural
66
language: en
77
tag:
8-
- Decoupling
8+
- Architecture
9+
- Decoupling
10+
- Domain
11+
- Inversion of control
12+
- Layered architecture
13+
- Modularity
14+
- Testing
915
---
1016

11-
## Also known as
12-
13-
* Hexagonal Architecture.
14-
1517
## Intent of Clean Architecture.
1618

17-
The clean architecture is a software design architectural style which ensures the software application is easy to understand, maintainable and can be extend easily as per business requirement.
19+
To organize the system so that the core business logic remains independent from external concerns and frameworks.
1820

1921
## Detailed Explanation of Clean Architecture Pattern with Real-World Examples
2022

21-
Real World.
23+
Real-world example
2224

23-
A real world example of clean architecture is like teh shopping mall example. There some employee is assigned to work on the filling of the products in the counter, one person is responsible for the billing purpose, one person is taking care of the security, one person is taking care of the product they have in storage. The work of every individual is separate and they are focussed on the specific task. Clean architecture also suggests to make the component separate and each component should perform some task. Clean Architecture proposes a layered architecture with clear boundaries between different system components to achieve independence of frameworks, UI, databases, and delivery mechanisms and the possibility to test in isolation.
25+
> Imagine a large pizza chain with multiple ordering channels—web, mobile app, phone calls, and in-store kiosks. The core “pizza domain” logic (calculating prices, preparing orders, managing loyalty points) is kept entirely separate from the user interfaces and storage mechanisms. As a result, the chain can add or change the ordering channel (for example, introducing a chatbot or swapping out the database) without altering the fundamental pizza-ordering rules, thanks to the layered boundaries and strict dependency rules of Clean Architecture.
2426
25-
In plain word
27+
In plain words
2628

27-
It helps to make the system more maintainable and easy to extend.
29+
> Clean Architecture is a software design approach that isolates the core business logic from external concerns (like databases, frameworks, or UI) through strict layering and clear boundaries, ensuring that changes in one layer don't ripple through the entire system.
2830
2931
Wikipedia says
3032

31-
> The clean architecture proposed by Robert C. Martin in 2012 combines the principles of the hexagonal architecture, the onion architecture and several other variants. It provides additional levels of detail of the component, which are presented as concentric rings. It isolates adapters and interfaces (user interface, databases, external systems, devices) in the outer rings of the architecture and leaves the inner rings for use cases and entities.
32-
>
33-
> The clean architecture uses the principle of dependency inversion with the strict rule that dependencies shall only exist between an outer ring to an inner ring and never the contrary.
34-
33+
> The clean architecture proposed by Robert C. Martin in 2012 combines the principles of the hexagonal architecture, the onion architecture and several other variants. It provides additional levels of detail of the component, which are presented as concentric rings. It isolates adapters and interfaces (user interface, databases, external systems, devices) in the outer rings of the architecture and leaves the inner rings for use cases and entities. The clean architecture uses the principle of dependency inversion with the strict rule that dependencies shall only exist between an outer ring to an inner ring and never the contrary.
3534
36-
## Clean architecture Class Diagram
37-
38-
![Clean Architecture](./etc/cleanArchitectureUMLDiagram.png "Clean Architecture class diagram")
39-
40-
## When to Use the Clean Architecture Pattern in Java
35+
Mind map
4136

42-
In all application we can use the clean architecture style and make the component separate and business logic separate from the UI and database.
37+
![Clean Architecture Mind Map](./etc/clean-architecture-mind-map.png)
4338

44-
## Real-World Applications of Chain of Responsibility Pattern in Java.
39+
Flowchart
4540

46-
In the application say Ecommerce application user gives teh order and the application is represented using teh clean architecture pattern.
47-
48-
There are facility like the **product** where user can see the product details like the price and the features, **Cart** user can add the product they have selected and the **Order** where user can see the total order and calculate the price of the order. Learn how to implement this design pattern in Java with the following code snippet.
41+
![Clean Architecture Flowchart](./etc/clean-architecture-flowchart.png)
4942

5043
## Programmatic Example of Clean Architecture Pattern
5144

52-
First we have the entity class like the `Product`, `Order` and teh `Cart`
45+
First, we define the core domain entities: `Product`, `Order`, and `Cart`. These classes capture the fundamental business logic and state.
46+
5347
```java
5448
public class Product {
5549
private String id;
@@ -93,7 +87,9 @@ public class Order {
9387
}
9488
}
9589
```
96-
The repository interfaces are created.
90+
91+
The repository interfaces are created to abstract data operations for each domain object, allowing us to switch out storage or persistence mechanisms without changing higher-level logic.
92+
9793
```java
9894
public interface CartRepository {
9995
void addItemToCart(String userId, Product product, int quantity);
@@ -114,8 +110,8 @@ public interface OrderRepository {
114110
}
115111
```
116112

113+
The in-memory data store implementations use simple collections to hold state. They demonstrate how easily we can replace or extend the data layer (e.g., swapping in a database) without affecting the domain logic.
117114

118-
The in memory data store in the cart and order.
119115
```java
120116
public class InMemoryCartRepository implements CartRepository {
121117
private final Map<String, List<Cart>> userCarts = new HashMap<>();
@@ -181,7 +177,8 @@ public class InMemoryProductRepository implements ProductRepository {
181177
}
182178
```
183179

184-
The order controller.
180+
The order controller coordinates the checkout process by using the use-case or service layer (`ShoppingCartService`).
181+
185182
```java
186183
public class OrderController{
187184
private final ShoppingCartService shoppingCartUseCase;
@@ -195,7 +192,10 @@ public class OrderController{
195192
}
196193
}
197194
```
198-
The cart controller.
195+
196+
The cart controller focuses on cart-related actions like adding or removing items and calculating totals.
197+
198+
199199
```java
200200
public class CartController {
201201
private final ShoppingCartService shoppingCartUseCase;
@@ -218,7 +218,8 @@ public class CartController {
218218
}
219219
```
220220

221-
The clean architecture in action.
221+
The clean architecture in action. In the `main` method, we wire up everything, simulating a typical user flow: items are added to the cart, the total is calculated, and finally an order is placed.
222+
222223
```java
223224
public static void main(String[] args) {
224225

@@ -245,33 +246,46 @@ public static void main(String[] args) {
245246
```
246247

247248
The output of the code.
249+
248250
```md
249251
Total: $2000.0
250252
Order placed! Order ID: ORDER-1743349969254, Total: $2000.0
251253
```
252254

253-
## Benefits and Trade-offs of Clean Architecture Pattern.
255+
## When to Use the Clean Architecture Pattern in Java
256+
257+
* When you want to keep business rules independent of UI, database, or any other external agency
258+
* When you need a high level of maintainability and testability in large Java applications
259+
* When you aim to enforce clear boundaries among application layers
260+
261+
## Real-World Applications of Clean Architecture Pattern in Java
262+
263+
* Large-scale enterprise systems in finance and insurance domains
264+
* Microservices-based architectures that prioritize decoupling and modular design
265+
* Java systems requiring stringent separation of concerns and domain-centered design
266+
267+
## Benefits and Trade-offs of Clean Architecture Pattern
254268

255269
Benefits:
256270

257-
The main benefits of the Clean Architecture involves -
258-
**Scalability** - It allows to add new features without any issue.
259-
**Modularity** - It makes the code loosely coupled and making the change in any component becomes easier.
260-
**Testability** - The architecture promotes unit testing, integration testing, and acceptance testing of different layers independently.
271+
* High maintainability by isolating core logic from infrastructure details
272+
* Enhanced testability through clear boundaries around the domain model
273+
* Flexibility in replacing or upgrading external components without affecting core logic
261274

262275
Trade-Offs:
263276

264-
Initially the design needs to be done with high precision and the UML diagram should cover all the architectural structure. It will in return help to make it more channelised and the code extensibility will increase.
277+
* Initial complexity from enforcing strict layers and boundaries
278+
* Potential overhead in smaller projects not requiring such rigid separation
279+
* Requires disciplined team adherence to architecture rules
265280

266281
## Related Java Design Patterns
267282

268-
* Dependency Injection - Dependency Injection (DI) is a key concept in Clean Architecture. It promotes loose coupling between classes by allowing dependencies to be injected rather than directly created by the class itself.
269-
* Singleton Pattern - The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. This is often used for shared services or resources, such as a configuration manager, logging service, or database connection pool.
283+
* [Dependency Injection](https://java-design-patterns.com/patterns/dependency-injection/): Facilitates decoupling layers by injecting dependencies rather than hard-coding them
284+
* [Layered Architecture](https://java-design-patterns.com/patterns/layered-architecture/): Both separate concerns into distinct tiers but Clean Architecture emphasizes strict dependency rules
285+
* [Hexagonal Architecture](https://java-design-patterns.com/patterns/hexagonal-architecture/): Similar focus on isolating core logic with ports and adapters
270286

271287
## References and Credits
272288

273-
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0pvKI)
274-
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/49NGldq)
275-
* [Pattern-Oriented Software Architecture, Volume 1: A System of Patterns](https://amzn.to/3PAJUg5)
276-
* [Refactoring to Patterns](https://amzn.to/3VOO4F5)
277-
* [Pattern languages of program design 3](https://amzn.to/4a4NxTH)
289+
* [Clean Architecture: A Craftsman's Guide to Software Structure and Design](https://amzn.to/3UoKkaR)
290+
* [Clean Code: A Handbook of Agile Software Craftsmanship](https://amzn.to/3wRnjp5)
291+
* [Domain-Driven Design: Tackling Complexity in the Heart of Software](https://amzn.to/3wlDrze)
113 KB
Loading
123 KB
Loading

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/App.java

+25-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
package com.iluwatar.cleanarchitecture;
226

327
import lombok.extern.slf4j.Slf4j;
@@ -39,7 +63,7 @@ public static void main(final String[] args) {
3963
cartController.addItemToCart(userId, "2", 2);
4064

4165
Order order = orderController.checkout(userId);
42-
LOGGER.info("Total: ${}" + cartController.calculateTotal(userId));
66+
LOGGER.info("Total: ${}", cartController.calculateTotal(userId));
4367

4468
LOGGER.info(
4569
"Order placed! Order ID: {}, Total: ${}", order.getOrderId(), order.getTotalPrice());

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Cart.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using ZK framework
4-
* licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartController.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using ZK framework
4-
* licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä
@@ -24,7 +22,6 @@
2422
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2523
* THE SOFTWARE.
2624
*/
27-
2825
package com.iluwatar.cleanarchitecture;
2926

3027
/**

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/CartRepository.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using ZK framework
4-
* licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryCartRepository.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using ZK framework
4-
* licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryOrderRepository.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using ZK framework
4-
* licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/InMemoryProductRepository.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using ZK framework
4-
* licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Order.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using ZK framework
4-
* licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderController.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using ZK framework
4-
* licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/OrderRepository.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using ZK framework
4-
* licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/Product.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using ZK framework
4-
* licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ProductRepository.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using ZK framework
4-
* licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/ShoppingCartService.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/*
2-
* This project is licensed under the MIT license.
3-
* Module model-view-viewmodel is using
4-
* ZK framework licensed under LGPL (see lgpl-3.0.txt).
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
53
*
64
* The MIT License
75
* Copyright © 2014-2022 Ilkka Seppälä

Diff for: clean-architecture/src/main/java/com/iluwatar/cleanarchitecture/package-info.java

+24
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,27 @@
1+
/*
2+
* This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt).
3+
*
4+
* The MIT License
5+
* Copyright © 2014-2022 Ilkka Seppälä
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
125
/**
226
* Provides classes and interfaces for the clean architecture pattern implementation.
327
*

0 commit comments

Comments
 (0)