两个版本,哪个更好?(更具可读性,可维护性。。。。。。)
第一版本代码:
package nc.bs.scap.business; import java.util.HashMap; import java.util.Map; import nc.bs.scap.abstractbusi.SCAPAbstractBusi; import nc.bs.scap.backfill.SCAPOnceBackFillBill; import nc.bs.trade.business.HYPubBO; import nc.bs.yc.pub.BackFillBill; import nc.vo.pub.AggregatedValueObject; import nc.vo.pub.BusinessException; import nc.vo.pub.compiler.PfParameterVO; import nc.vo.pub.lang.UFBoolean; import nc.vo.scap.trynotice.TrynoticeBVO; import nc.vo.scap.tryrecord.TryrecordBVO; import nc.vo.yc.utils.YCPubUtils; /** * 试用纪录单业务类 * * @author limx * @Description TODO * @date 2011-4-10 上午09:38:47 * */ public class TryrecordBusi extends SCAPAbstractBusi { @Override public void approve(AggregatedValueObject aggVo, PfParameterVO vo) throws BusinessException { super.approve(aggVo, vo); backFillTryResult(aggVo, vo, true);// 回写试用结果 } @Override public void unApprove(AggregatedValueObject aggVo, PfParameterVO vo) throws BusinessException { super.unApprove(aggVo, vo); backFillTryResult(aggVo, vo, false);// 删除试用结果 } /** * 回写试用记录单的试用结果 * * @param aggVo * 聚合VO * @param parameVo * @param isApprove * 审核和弃审开关 * @throws BusinessException */ public void backFillTryResult(AggregatedValueObject aggVo, PfParameterVO parameVo, boolean isApprove) throws BusinessException { TryrecordBVO[] bodyVOs = (TryrecordBVO[]) aggVo.getChildrenVO(); Map<String, TryrecordBVO> backFillTryrecordBVOS = new HashMap<String, TryrecordBVO>(); for (TryrecordBVO tryrecodeSelectedBVO : bodyVOs) { backFillTryrecordBVOS.put(tryrecodeSelectedBVO.getVlastbillrowid(), tryrecodeSelectedBVO);// 添加回写数据 } if (!YCPubUtils.isEmpty(backFillTryrecordBVOS.keySet())) { HYPubBO hyPubBO = new HYPubBO(); // 查询要进行回写的试用通知单表体记录 TrynoticeBVO[] trynoticeBVOS = (TrynoticeBVO[]) hyPubBO.queryByCondition(TrynoticeBVO.class, TrynoticeBVO.PK_TRYNOTICE_B + " in (" + YCPubUtils.getUnionSQLString(backFillTryrecordBVOS.keySet()) + ")"); for (TrynoticeBVO trynoticeBVO : trynoticeBVOS) { TryrecordBVO tryrecodeSelectedBVO = backFillTryrecordBVOS.get(trynoticeBVO.getPk_trynotice_b()); if (isApprove) { trynoticeBVO.setVtryresult(tryrecodeSelectedBVO.getVtryresult());// 审核回写 trynoticeBVO.setIstryfinish(UFBoolean.TRUE); } else { trynoticeBVO.setVtryresult(null);// 弃审回写 trynoticeBVO.setIstryfinish(null); } } hyPubBO.updateAry(trynoticeBVOS); } } /* * 创建单据回写类 */ @Override protected BackFillBill createBackFill() throws BusinessException { return new SCAPOnceBackFillBill(); } }
重构后的代码:
package nc.bs.scap.business; import java.util.HashMap; import java.util.Map; import nc.bs.scap.abstractbusi.SCAPAbstractBusi; import nc.bs.scap.backfill.SCAPOnceBackFillBill; import nc.bs.trade.business.HYPubBO; import nc.bs.yc.pub.BackFillBill; import nc.vo.pub.AggregatedValueObject; import nc.vo.pub.BusinessException; import nc.vo.pub.compiler.PfParameterVO; import nc.vo.pub.lang.UFBoolean; import nc.vo.scap.trynotice.TrynoticeBVO; import nc.vo.scap.tryrecord.TryrecordBVO; import nc.vo.yc.utils.YCPubUtils; /** * 试用纪录单业务类 * * @author limx * @Description TODO * @date 2011-4-10 上午09:38:47 * */ public class TryrecordBusi extends SCAPAbstractBusi { @Override public void approve(AggregatedValueObject aggVo, PfParameterVO vo) throws BusinessException { super.approve(aggVo, vo); Map<String, String> tryResult = getTryResutl(aggVo); TrynoticeBVO[] trynoticeBVOs = getTrynoticeBVOs(tryResult); backFillTryResultWhenApprove(tryResult, trynoticeBVOs); } @Override public void unApprove(AggregatedValueObject aggVo, PfParameterVO vo) throws BusinessException { super.unApprove(aggVo, vo); Map<String, String> tryResult = getTryResutl(aggVo); TrynoticeBVO[] trynoticeBVOs = getTrynoticeBVOs(tryResult); backFillTryResultWhenUnapprove(trynoticeBVOs); } /** * 返回 map<上层单据行id,试用记录单的试用结果> * @param aggVO * @return */ private Map<String, String> getTryResutl(AggregatedValueObject aggVO) { // 需要回写的试用结果 TryrecordBVO[] bodyVOs = (TryrecordBVO[])aggVO.getChildrenVO(); Map<String, String> tryResult = new HashMap<String, String>(); for(TryrecordBVO backFillTryResultBVO : bodyVOs) { tryResult.put(backFillTryResultBVO.getVlastbillrowid(), (String)backFillTryResultBVO.getAttributeValue(TryrecordBVO.VTRYRESULT)); } return tryResult; } /** * 返回被参照的试用通知单表体 * @param tryResult * @return * @throws BusinessException */ private TrynoticeBVO[] getTrynoticeBVOs(Map<String, String> tryResult) throws BusinessException { // 被参照的试用通知单 return (TrynoticeBVO[]) getHYPubBO().queryByCondition(TrynoticeBVO.class, TrynoticeBVO.PK_TRYNOTICE_B + " in (" + YCPubUtils.getUnionSQLString(tryResult.keySet()) + ")"); } /** * 审核回写试用结果 * @param tryResult * @param trynoticeBVOs * @throws BusinessException */ private void backFillTryResultWhenApprove(Map<String, String> tryResult, TrynoticeBVO[] trynoticeBVOs) throws BusinessException { for(TrynoticeBVO trynoticeBVO : trynoticeBVOs) { trynoticeBVO.setVtryresult(tryResult.get(trynoticeBVO.getPk_trynotice_b())); trynoticeBVO.setIstryfinish(UFBoolean.TRUE); } getHYPubBO().updateAry(trynoticeBVOs); } /** * 弃审回写试用结果 * @param trynoticeBVOs * @throws BusinessException */ private void backFillTryResultWhenUnapprove(TrynoticeBVO[] trynoticeBVOs) throws BusinessException { for(TrynoticeBVO trynoticeBVO : trynoticeBVOs) { trynoticeBVO.setVtryresult(null); trynoticeBVO.setIstryfinish(null); } getHYPubBO().updateAry(trynoticeBVOs); } private HYPubBO hyPubBO; private HYPubBO getHYPubBO() { if(YCPubUtils.isEmpty(hyPubBO)) { hyPubBO = new HYPubBO(); } return hyPubBO; } /* * 创建单据回写类 */ @Override protected BackFillBill createBackFill() throws BusinessException { return new SCAPOnceBackFillBill(); } }