最近在看Effective java 第二版这本书,其中看到第21条:用函数对象表示策略和第30条:用enum代替int常量,发现原先有个项目当中可以用到策略枚举,于是把以前的代码拿出来,重新改版成策略枚举的形式,并把代码发布在这里,以供日后学习使用。另外对这段代码的实践,也使自己澄清了一个误区,原先一直以为j2ee的MVC模式的开发都是固定的模式:Action+service+dao,dao写操作数据库的类,service调用dao层,action再调用service层,而且由于使用spring的注入,以前一直都是这么写
@Resource private IMbPrivilegeService mbPrivilegeService;
由于注入关系,用到一个对象的时候只要从spring容器里边拿出来就可以了,不需要去new一个Action,service,dao等,但是看到很多设计模式当中都new一个对象出来,所以一直都认为j2ee的MVC这种开发模式用到的其他的设计模式很少。因而在开发j2ee项目当中一直使用的MCV这一套思路,写出的代码质量也不高。很惭愧,现在刚刚开始转入android开发才想明白这个事情:只要用一种方法从spring当中获取到对象的实例,然后在别的类里边获取到这个实例的引用,那么其他的设计模式在j2ee的开发当中也是很好使用的。就像以下的写的关于策略枚举一样,其中也调用到操作业务的service层,只要用
SpringUtil.getBean("refstatService");
就能能把service实例从spring容器中拿出来,为整个枚举提供服务。
以下为策略枚举的源码
/** * 策略枚举 * @author Administrator */ public enum MbActivity { SUNDAY(Calendar.SUNDAY, ActivityType.ScratchSeven), MONDAY(Calendar.MONDAY, ActivityType.ScratchSeven), TUESDAY(Calendar.TUESDAY, ActivityType.ScratchSeven), WEDNESDAY(Calendar.WEDNESDAY, ActivityType.DownLoad), THURSDAY(Calendar.THURSDAY, ActivityType.DownLoad), FRIDAY(Calendar.FRIDAY, ActivityType.SecondKill), SATURDAY(Calendar.SATURDAY, ActivityType.ScratchSeven); private static Log log= LogFactory.getLog(MbActivity.class); private static Calendar calendar = Calendar.getInstance(); private final static IRefstatService refstatService = (IRefstatService)SpringUtil.getBean("refstatService"); private final static IMbPrivilegeService mbPrivilegeService = (IMbPrivilegeService)SpringUtil.getBean ("mbPrivilegeService"); private ActivityType type; private int week;//星期 private String resultCode;//结果状态码 private String mobile;//手机号 private String yxId;//id private MbActivity(int week, ActivityType type){ this.week = week; this.type = type; } //策略 public boolean strategy(String mobile,String id) throws ServiceException{ this.init(mobile, id); this.updatePV(); type.checkValid(); this.downLoad(); this.insertLog(); return true; } public static MbActivity valueOf(int week){ for(MbActivity mb: values()){ if (week == mb.week){ return mb; } } throw new IllegalArgumentException("week:"+week); } //初始化 private void init(String mobile,String id) throws ServiceException{ Assert.hasLength(mobile,"手机号为空"); Assert.hasLength(id,"id为空"); CmccSpecialPreferenceVO vo = null; try { vo = mbPrivilegeService.getMbPrivilegeById(id); } catch (ServiceException e) { e.printStackTrace(); } Assert.notNull(vo); Assert.hasLength(vo.getPrivilegeId(),"yxId为空"); this.yxId = vo.getPrivilegeId(); this.mobile = mobile; } //下发短信 private void downLoad() throws ServiceException{ try { DownResult downYxplan = mbPrivilegeService.downYxplan(mobile, yxId); resultCode = downYxplan.getResultCode(); } catch (Exception e) { e.printStackTrace(); resultCode = "-1"; } } /** * 记录日志 * @throws ServiceException */ private void insertLog() throws ServiceException{ IMbPrivilegeLogService mbPrivilegeLogService = (IMbPrivilegeLogService)SpringUtil.getBean("mbPrivilegeLogService"); //记录日志,记录成功与失败,以及失败的原因 TbCmccSpecialPreferenceLog log = new TbCmccSpecialPreferenceLog(); log.setMobile(mobile); log.setPrivilegeId(yxId); log.setResult(resultCode); log.setCreateTime(new Date()); String ip = IpUtil.getRequesterIp(ServletActionContext.getRequest()); log.setIp(ip); log.setPrivilegeNo(this.findPV()+""); mbPrivilegeLogService.insertMbPrivilegeLog(log); } /** * 更新用户参与数 (+1) * @throws ServiceException */ private void updatePV() throws ServiceException{ refstatService.updateRefstatPv("2", 1); } private enum ActivityType{ //miaosha SecondKill { boolean checkValid() throws ServiceException{ int hour = calendar.get(Calendar.HOUR_OF_DAY); log.debug("现在时间:"+hour); if ((10<=hour && hour<=12) || (14<=hour && hour<=16) || 18<=hour && hour<=20){ log.debug("时间正确,可以进行miaosha"); }else{ throw new ServiceException("尊敬的客户,周五必须在 10点-12点, 14点-16点,18点-20点时间段内才能参加miaosha活动!"); } return true; } }, //qiangqi ScratchSeven{ boolean checkValid() throws ServiceException{ int hour = calendar.get(Calendar.HOUR_OF_DAY); if (hour <= 10){ throw new ServiceException("hour:"+hour+",不在qiangqi的时间段"); } Integer downCount = findPV();//参与数 log.debug("第"+downCount+"位参加qiangqi的用户"); if (!downCount.toString().contains("7")){ throw new ServiceException("尊敬的客户,本次您qiangqi排位第 "+downCount+" 名,排位数字未含“ 7 ” ,请您再接再厉!"); } return false; } }, //xiazai DownLoad{ boolean checkValid() throws ServiceException{ int hour = calendar.get(Calendar.HOUR_OF_DAY); if (hour <= 10){ throw new ServiceException("hour:"+hour+",不在xiazai的时间段"); } return true; } }; abstract boolean checkValid() throws ServiceException; } /** * 获取当前用户参与数 * @return * @throws ServiceException */ private static int findPV() throws ServiceException{ TbRefstat tbRefstat = refstatService.findRefstatById("2"); return tbRefstat.getPv(); } public static void main(String[] args) { try { //调用 MbActivity.valueOf(Calendar.FRIDAY).strategy("13580999", "1111"); } catch (ServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }