|
| 1 | +package com.iluwatar.cleanarchitecture; |
| 2 | + |
| 3 | +import lombok.extern.slf4j.Slf4j; |
| 4 | + |
| 5 | +/** |
| 6 | + * Clean Architecture ensures separation of concerns by organizing code into layers, making it scalable and maintainable. |
| 7 | + * |
| 8 | + * In the example there are Entities (Core Models) – Product, Cart, Order handle business logic. |
| 9 | + * Use Cases (Application Logic) – ShoppingCartService manages operations like adding items and checkout. |
| 10 | + * Interfaces & Adapters – Repositories (CartRepository, OrderRepository) abstract data handling, while controllers (CartController, OrderController) manage interactions. |
| 11 | + */ |
| 12 | +@Slf4j |
| 13 | +public class App { |
| 14 | + |
| 15 | + /** |
| 16 | + * Program entry point. |
| 17 | + * |
| 18 | + * @param args command line args |
| 19 | + */ |
| 20 | + public static void main(String[] args) { |
| 21 | + |
| 22 | + ProductRepository productRepository = new InMemoryProductRepository(); |
| 23 | + CartRepository cartRepository = new InMemoryCartRepository(); |
| 24 | + OrderRepository orderRepository = new InMemoryOrderRepository(); |
| 25 | + |
| 26 | + ShoppingCartService shoppingCartUseCase = |
| 27 | + new ShoppingCartService(productRepository, cartRepository, orderRepository); |
| 28 | + |
| 29 | + CartController cartController = new CartController(shoppingCartUseCase); |
| 30 | + OrderController orderController = new OrderController(shoppingCartUseCase); |
| 31 | + |
| 32 | + String userId = "user123"; |
| 33 | + cartController.addItemToCart(userId, "1", 1); |
| 34 | + cartController.addItemToCart(userId, "2", 2); |
| 35 | + |
| 36 | + LOGGER.info("Total: ${}" + cartController.calculateTotal(userId)); |
| 37 | + |
| 38 | + Order order = orderController.checkout(userId); |
| 39 | + LOGGER.info("Order placed! Order ID: {}, Total: ${}", order.getOrderId(), order.getTotalPrice()); |
| 40 | + } |
| 41 | +} |
0 commit comments