Skip to content

Instantly share code, notes, and snippets.

@Phury
Created March 14, 2023 11:18
Show Gist options
  • Save Phury/27d0c041c5975f656006740c2ea5572f to your computer and use it in GitHub Desktop.
Save Phury/27d0c041c5975f656006740c2ea5572f to your computer and use it in GitHub Desktop.
super power ninja turbo architecture
// couche de présentation
public class TaskController {
private TaskService taskService;
public void addTask(String title, String description) {
Task task = new Task(title, description);
taskService.addTask(task);
}
public List<Task> getTasks() {
return taskService.getTasks();
}
// ...
}
// couche de contrôle
public class TaskService {
private TaskRepository taskRepository;
public void addTask(Task task) {
taskRepository.save(task);
}
public List<Task> getTasks() {
return taskRepository.findAll();
}
// ...
}
// couche de service
public interface TaskRepository {
void save(Task task);
List<Task> findAll();
}
// couche de persistance
public class InMemoryTaskRepository implements TaskRepository {
private List<Task> tasks = new ArrayList<>();
@Override
public void save(Task task) {
tasks.add(task);
}
@Override
public List<Task> findAll() {
return Collections.unmodifiableList(tasks);
}
// ...
}
public class Order implements AggregateRoot {
private OrderId orderId;
private CustomerId customerId;
private List<OrderLine> orderLines;
public Order(CustomerId customerId) {
this.orderId = new OrderId();
this.customerId = customerId;
this.orderLines = new ArrayList<>();
}
public void addOrderLine(ProductId productId, int quantity) {
OrderLine orderLine = new OrderLine(productId, quantity);
orderLines.add(orderLine);
}
// ...
}
public class Product implements Entity<ProductId> {
private ProductId id;
private String name;
private String description;
private Money price;
public Product(ProductId id, String name, String description, Money price) {
this.id = id;
this.name = name;
this.description = description;
this.price = price;
}
// ...
}
public class OrderService {
private OrderRepository orderRepository;
private ProductService productService;
public Order createOrder(CustomerId customerId, List<OrderLineRequest> orderLineRequests) {
Order order = new Order(customerId);
for (OrderLineRequest orderLineRequest : orderLineRequests) {
Product product = productService.getProductById(orderLineRequest.getProductId());
order.addOrderLine(product.getId(), orderLineRequest.getQuantity());
}
orderRepository.save(order);
return order;
}
// ...
}
// port d'entrée
public interface BookController {
void addBook(String title, String author);
List<Book> getBooks();
// ...
}
// port de sortie
public interface BookRepository {
void save(Book book);
List<Book> findAll();
// ...
}
// adaptateur d'entrée
public class RestBookController implements BookController {
private BookService bookService;
public void addBook(String title, String author) {
Book book = new Book(title, author);
bookService.addBook(book);
}
public List<Book> getBooks() {
return bookService.getBooks();
}
// ...
}
// adaptateur de sortie
public class InMemoryBookRepository implements BookRepository {
private List<Book> books = new ArrayList<>();
public void save(Book book) {
books.add(book);
}
public List<Book> findAll() {
return Collections.unmodifiableList(books);
}
// ...
}
// cœur de l'application
public class BookService {
private BookRepository bookRepository;
public void addBook(Book book) {
bookRepository.save(book);
}
public List<Book> getBooks() {
return bookRepository.findAll();
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment