時(shí)間:2023-06-12 10:57:01 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-06-12 10:57:01 來(lái)源:網(wǎng)站運(yùn)營(yíng)
模板模式(Java版):public interface BaseTrainService<K, V> { V execute(K vo);}
@Slf4jpublic abstract class CommonTrainService<K, V> implements BaseTrainService<K, V> { // 預(yù)處理 protected abstract boolean preHandle(K vo); // 核心處理流程 protected abstract V handle(K vo); // 后處理 protected abstract V postHandle(K vo, V dto); @Override public final V execute(K vo) { V dto = null; try { boolean flag = preHandle(vo); if (flag) { dto = handle(vo); } dto = postHandle(vo, dto); } catch (Exception e) { log.error("處理訓(xùn)練任務(wù)異常", e); } return dto; }}
@Slf4jpublic class SimpleTrainService extends CommonTrainService<TrainVO, Response<TrainDTO>> { @Override protected boolean preHandle(TrainVO vo) { return true; } @Override protected Response<TrainDTO> handle(TrainVO vo) { TrainDTO result = new TrainDTO(); ... return Response.success(result); } @Override protected Response<TrainDTO> postHandle(TrainVO vo, Response<TrainDTO> dto) { if (dto == null) { return Response.fail("創(chuàng)建訓(xùn)練任務(wù)失敗"); } return dto; } }
@Slf4jpublic class ComplexTrainService extends CommonTrainService<TrainVO, Response<TrainDTO>> { @Override protected boolean preHandle(TrainVO vo) { List<TrainRecord> list = .... return CollectionUtils.isEmpty(list); } @Override protected Response<TrainDTO> handle(TrainVO vo) { TrainDTO result = new TrainDTO(); ... return Response.success(result); } @Override protected Response<TrainDTO> postHandle(TrainVO vo, Response<TrainDTO> dto) { if (dto == null) { return Response.fail("創(chuàng)建訓(xùn)練任務(wù)失敗"); } return dto; } }
public class Client { public Response<TrainDTO> simpleTrainTask(TrainVO vo){ ComplexTrainService service = new ComplexTrainService(); return service.execute(vo); }}
關(guān)鍵詞:模式,模板
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。