我正在阅读系统代码,并试图了解服务层设计。
我观察到,对于每个对象(要保存到数据库的实体),将创建一组相关的类/实现,如下所示:
根据购物车示例,对于客户购买的每种产品,我将必须执行以下交易:
我应该如何设计/实施?
方法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);
}
}
方法#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.getNotification);
}
}
方法2是解决之道,因为它将您的业务逻辑封装在服务层中,从而使您可以遵循DRY(即,请勿重复自己)原则。
假设今天,您只为桌面网站实现功能(下订单)。将来,如果您需要将其添加到Android应用程序中,那么如果使用方法2 ,就可以重用ShoppingCartService 。如果要使用方法1,则必须在应用程序中复制业务逻辑。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句