业务是主线和事件组合。
主线是什么?主线就是在完成用户的业务目标时,所涉及到的活动。
事件是什么?在每一个活动中,相关事物的状态变化。
回顾以前的编程经验,我们关注活动,忽略事件。
举一个常见的例子来说。小明在购物网站购物,进行支付并成功后,购物网站要做件事: 1 、更新订单状态, 2 、插入支付记录 3 、更新订单日志。 4 、通知支付系统,回调成功。
通常的做法如下:
public void successToPay(PurchaseOrder order,PayResult result){ //1、更改支付状态 order.setPayStatus(PayStatus.SUCCESS); orderRepository.save(order); //2、插入支付记录 PayRecord record= RecordFactory.getSuccessRecord(order,result); recordRepository.insert(record); //3、插入订单日志 OrderLog log = OrderFactory.getPaidLog(order); logRepository.insert(log); //4、通知财务系统,支付成功 NotifyLog notifylog= NotifyFacotry.getSuccessNotify(order); nofifyService.notify(notifylog); }
上述做法没有合适的关注点,无法把主线找到,随着随着业务扩大,修改代码是一件高风险的事情。
换另一种方式来处理这些问题。
1、发现该业务的主线。
2、分析主线中的每一个活动会给系统带来什么变化。而这些变化就是主线所要传递的消息。
3、把事件通知给相关对象。
以上述例子为例
可以得知 【更新订单状态】和【插入支付记录】是整个业务的主线。 【更新订单日志】和【通知支付系统,回到成功】是由于订单支付成功而引发的。
因此在代码实现中,可以这样写
public class PurchaseOrder { public void notifyOrderPaid(List<OrderListener> listeners){ for(OrderListener listener:listeners){ listener.onOrderSuccessPaid(listener); } } } public interface OrderListener{ public void onOrderSuccessPaid(PurchaseOrder order); } public class LogListener implements OrderListener{ private LogRepository logRepository; public void onOrderSuccessPaid(PurchaseOrder order){ Log log = OrderFactory.getPaidLog(order); logRepository.insert(log); } } public class FinanceListener implements OrderListener{ private FinanceService financeService; public void onOrderSuccessPaid(PurchaseOrder order){ NotifyLog notifylog= NotifyFacotry.getSuccessNotify(order); nofifyService.notify(notifylog); } } public class OrderService{ public void successToPay(PurchaseOrder order,PayResult result){ //主线 order.setPayStatus(PayStatus.SUCCESS); orderRepository.save(order); PayRecord record = RecordFactory.getSuccessPaidRecord(order,result); payRecordRepository.sae(record); //事件 List<OrderListener> listeners =new ArrayList<OrderListener>(); listeners.add(new FinanceListener()); listeners.add(new LogListener()); order.notifyOrderPaid(listeners); } }
一旦有新的业务增加时,应该考虑到它是属于主线,还是属于事件。我们的代码就不容易走向混乱。