应用场景很多,尤其是在框架设计中,提供了一个方便的开发程序的模板,你只要实现模板中的一些接口或者方法就能完成一个复杂的任务。
结构类图
AbstractTemplate:定义一个完整的框架,方法的调用顺序已经确定,但会定义一些抽象的方法留给子类去实现。
SubTemplate:实现抽象模板中定义的抽象方法,从而形成一个完整的流程逻辑。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public TradeFlowActionResult execute(TradeFlowActionParam param, Map context) throws ServiceException { try { this.validateBusinessLogic(param, context); } catch (ServiceException ex) { sendGoodsLog.info("SendGoodsAction->validateBusinessLogic got exception. param is " + param, ex); throw ex; } catch (RuntimeException ex) { sendGoodsLog.info("SendGoodsAction->validateBusinessLogic got runtime exception. param is " + param, ex); throw ex; } try { this.sendGoods(param, context); } catch (ServiceException ex) { sendGoodsLog.info("SendGoodsAction->sendGoods got exception. param is " + param, ex); throw ex; } catch (RuntimeException ex) { sendGoodsLog.info("SendGoodsAction->sendGoods got runtime exception. param is " + param, ex); throw ex; } try { this.addition(param, context); } catch (ServiceException ex) { sendGoodsLog.info("SendGoodsAction->addition got exception. param is " + param, ex); throw ex; } catch (RuntimeException ex) { sendGoodsLog.info("SendGoodsAction->addition got runtime exception. param is " + param, ex); throw ex; } return null; }
|
上面提到的三个抽象方法(业务逻辑校验、卖家发货业务逻辑、补充业务)都是在子类中实现的。
使用模板方法模式既控制了主流程结构,又不失灵活性,可以让使用者在其基础上定制开发。