原函数:
package function_object; /** * 帐目类 */ public class Account{ /** * 计量方法(需要三个参数) */ public int gamma(int inputVal, int quantity, int yearToDate){ int importantValue1 = (inputVal * quantity) + delta(); int importantValue2 = (inputVal * yearToDate) + 100; if((yearToDate - importantValue1) > 100) importantValue2 -= 20; int importantValue3 = importantValue2 * 7; // and so on. return importantValue3 - 2 * importantValue1; } /* * 变量: 增量幅度值 * 重构方法之: 用查询方法代替临时变量 */ public int delta(){//此处设置public, 以便函数对象获取(同包下可以设置default, 最低可见度原则) return 100; } }
重构之创建函数对象:
package function_object; /** * 计量方法对象(函数对象) */ public class Gamma{ private final Account account; private int inputVal; private int quantity; private int yearToDate; private int importantValue1; private int importantValue2; private int importantValue3; /** * 计量方法对象构造器, 计量方法需要三个参数, 并且还需 * 多传一个原函数所在对象参数, 用以获得依赖的属性或方法 */ public Gamma(int inputValArg, int quantityArg, int yearToDateArg, Account source){ this.account = source; this.inputVal = inputValArg; this.quantity = quantityArg; this.yearToDate = yearToDateArg; } /** * 计量方法 */ public int compute(){ this.importantValue1 = (inputVal * quantity) + account.delta(); this.importantValue2 = (inputVal * yearToDate) + 100; if((yearToDate - this.importantValue1) > 100) this.importantValue2 -= 20; this.importantValue3 = this.importantValue2 * 7; // and so on. return this.importantValue3 - 2 * this.importantValue1; } }
重够后原函数与现函数比较:
package function_object; /** * 帐目类 */ public class Account{ /** * 计量方法(需要三个参数)(重构前) */ public int gamma(int inputVal, int quantity, int yearToDate){ int importantValue1 = (inputVal * quantity) + delta(); int importantValue2 = (inputVal * yearToDate) + 100; if((yearToDate - importantValue1) > 100) importantValue2 -= 20; int importantValue3 = importantValue2 * 7; // and so on. return importantValue3 - 2 * importantValue1; } /** * 计量方法(重构后) */ public int gamma_n(int inputVal, int quantity, int yearToDate){ return new Gamma(inputVal, quantity, yearToDate, this).compute(); } /* * 变量: 增量幅度值 * 重构方法之: 用查询方法代替临时变量 */ public int delta(){//此处设置public, 以便函数对象获取(同包下可以设置default, 最低可见度原则) return 100; } }
真实项目中的重构:
xxxServiceImpl中的一个业务方法:
/** * 发起者延长时间 */ public String orderOvertime(PainterOrderFlowEntity from){ return new OrderOvertime(this, from).compute(); } public MySpaceMapper getMySpaceMapper(){ return this.mySpaceMapper; }
函数对象:
package com.qcacg.util.ztools.order.FunctionObjet; import com.qcacg.entity.PainterOrderEntity; import com.qcacg.entity.PainterOrderFlowEntity; import com.qcacg.service.system.impl.MySpaceServiceImpl; import com.qcacg.util.ztools.collection_util.CollectionFactory; import com.qcacg.util.ztools.timeData.Day; import com.qcacg.util.ztools.timeData.Month; import com.qcacg.util.ztools.timeData.Year; import java.sql.Timestamp; import java.util.Calendar; import java.util.Date; import java.util.Map; /** * 订单延长函数对象 */ public class OrderOvertime { private MySpaceServiceImpl mySpaceServiceImpl; private PainterOrderFlowEntity from; private PainterOrderEntity findpainterOrderEntity; private Date fromDate; private Calendar fromCalendar; private int inputYear; private int inputMonth; private int inputDay; private Date sqlDate; private Calendar sqlCalendar; private int sqlYear; private int sqlMonth; private int sqlDay; public OrderOvertime(MySpaceServiceImpl mySpaceServiceImpl, PainterOrderFlowEntity from){ this.mySpaceServiceImpl = mySpaceServiceImpl; this.from = from; } public String compute(){ //过滤非法客户延长时间 customerExtendTimeFilter(from); //extendTime是否超出范围 findpainterOrderEntity = mySpaceServiceImpl.getMySpaceMapper().findOrderEntityByOrderId(from.getOrderId()); //传入的 fromDate = from.getExtendTime(); fromCalendar = Calendar.getInstance(); fromCalendar.setTime(fromDate); //如果范围超限,抛出非首检异常 inputYear = new Year(fromCalendar.get(Calendar.YEAR)).getData(); inputMonth = new Month(fromCalendar.get(Calendar.MONTH) + 1).getData(); inputDay = new Day(fromCalendar.get(Calendar.DATE)).getData(); //sql的 sqlDate = findpainterOrderEntity.getDeliveryTime(); sqlCalendar = Calendar.getInstance(); sqlCalendar.setTime(sqlDate); sqlYear = sqlCalendar.get(Calendar.YEAR); sqlMonth = sqlCalendar.get(Calendar.MONTH) + 1; sqlDay = sqlCalendar.get(Calendar.DATE); //进行过滤 if (inputYear<sqlYear || inputMonth<sqlMonth || inputDay<=sqlDay){ return "请选择正确时间"; } //加工传入的日期 fromCalendar.set(fromCalendar.get(Calendar.YEAR),fromCalendar.get(Calendar.MONTH),fromCalendar.get(Calendar.DATE),23,59,55); from.setExtendTime(fromCalendar.getTime()); //正常订单(1进行中) 可以延长交付时间 if (findpainterOrderEntity!=null && 1==findpainterOrderEntity.getStatus()){ //更新订单表 交付时间 Map<String,Object> param = CollectionFactory.newHashMap(); param.put("orderId",from.getOrderId()); param.put("deliveryTime",from.getExtendTime()); mySpaceServiceImpl.getMySpaceMapper().updateOrderByOrderId(param); //插入 延长流程实体(画师只读) PainterOrderFlowEntity orderFlowEntity = new PainterOrderFlowEntity(null,from.getOrderId(),"延长",new Timestamp(System.currentTimeMillis()),null,"","",from.getExtendTime(),0,3); mySpaceServiceImpl.getMySpaceMapper().insertOrderFlowEntity(orderFlowEntity); return "延长成功"; } return "非进行中订单无法延长"; } /* 过滤非法客户延长时间 */ private void customerExtendTimeFilter(PainterOrderFlowEntity from){ if (from.getOrderId()==null || from.getExtendTime()==null){ throw new RuntimeException("参数异常"); } } }