I am reading the codes of a legacy system and trying to understand the service layer design.
I observed that for every object(entity to be saved to the database), a set of related classes/implementations will be created as follow:
- Product.java
- ProductDAO.java (Interface class)
- ProductDAOImpl.java
- ProductService.java (Interface class)
- ProductServiceImpl.java
Based on a shopping cart example, for every product the customer purchased, I will have to perform the following transaction:
- Decrease the quantity of the product
- Save the order
- Create a notification record for the batch job to send an email to notify the customer
How should I design/implement it?
Method #1
public class ShoppingCartPageBeanA {
private ProductService productService;
private OrderService orderService;
private NotificationService notificationService;
public void submit() {
productService.decreaseProductTotalQuantity(product);
orderService.saveOrder(order);
notificationService.saveNotification(notification);
}
}
Method #2
public class ShoppingCartPageBeanB {
private ShoppingCartService shoppingCartService;
public void submit() {
shoppingCartService.saveOrder(order);
}
}
public class ShoppingCartServiceImpl {
private ProductDAO productDAO;
private OrderDAO orderDAO;
private NotificationDAO notificationDAO;
public void saveOrder(Order order) {
productDAO.decreaseProductTotalQuantity(order.getProduct);
orderDAO.saveOrder(order);
notificationDAO.saveNotification(order);
}
}
Aucun commentaire:
Enregistrer un commentaire