vendredi 10 juin 2016

java service layer design implementation issue

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:

  1. Product.java
  2. ProductDAO.java (Interface class)
  3. ProductDAOImpl.java
  4. ProductService.java (Interface class)
  5. ProductServiceImpl.java

Based on a shopping cart example, for every product the customer purchased, I will have to perform the following transaction:

  1. Decrease the quantity of the product
  2. Save the order
  3. 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