商贸网站建设,房产机构网站建设目标定位,个人网页制作源代码格式,潮州vi设计公司1.前言
最近接到要修改的业务功能#xff0c;这个业务增删改查很多功能都需要校验时间#xff0c;比如#xff1a;
1.失效时间不能超过自己父表的失效时间#xff0c;
2.失效时间不能是当前时间
3.失效时间不能早于生效时间
类似这样的不同的判断还有很多#xff0c;…1.前言
最近接到要修改的业务功能这个业务增删改查很多功能都需要校验时间比如
1.失效时间不能超过自己父表的失效时间
2.失效时间不能是当前时间
3.失效时间不能早于生效时间
类似这样的不同的判断还有很多我就先举这三种例子因为这是在一个管理功能也许未来还有很多的这样的规则处理所以需要好好的设计下。
2.设计实现
我想着对于这种校验是一种规则而对于时间的校验就是时间校验规则所以我们定义个接口某某业务时间规则接口并定义了两个方法一个是对传过来的两个时间的校验范围一个是对当前时间及另一个传输过来的时间校验范围如下伪代码
public interface XxBussinessDateRule{/**传入两个时间进行校验*/boolean dateScopeValidate(String dateOne,String dateTwo,MathSymbolsEnum mathSymbols);/**传入时间与当前时间进行校验*/boolean dateScopeValidateByCurrent(String date,MathSymbolsEnum mathSymbols);
}
MathSymbolsEnum枚舉類是专门处理数学符号的如大于()小于()小于等于()等等等等
传输这个值才能更灵活处理校验规则。
public enum MathSymbolsEnum{/**大于*/GREATER_THAN,/**小于*/LESS_THAN,/**小于等于*/EQUAL_OR_LESS_THAN,/**大于等于*/EQUAL_OR_GREATER_THAN,/**等于*/EQUAL,/**不等于*/NOT_EQUAL,
}
DefaultxxBussinessRule类是xxBussinessRule的实现类因为只有此一个实现类所以名称里加一个Default这个类主要是对时间的校验需要根据枚举类的符号进行判断如果大于就调用此类私有方法dateValidateGreaterThan如果小于就找私有方法dateValidateLessThan用于处理不同情况的校验。
在我原有的实现里这里调用的是不同工具类的处理方法。伪代码呢就直接写死true,False返回你可随意实现
Component
public class DefaultxxBussinessDateRule implements XxBussinessDateRule {/*** 传入两个时间进行校验*/Overridepublic boolean dateScopeValidate(String dateOne, String dateTwo, MathSymbolsEnum mathSymbols) {// 大于if (mathSymbols.name().equals(GREATER_THAN)) {return dateValidateGreaterThan(dateOne, dateTwo);}// 小于if (mathSymbols.name().equals(LESS_THAN)) {return dateValidateLessThan(dateOne, dateTwo);}return false;}/*** 传入时间与当前时间进行校验*/Overridepublic boolean dateScopeValidateByCurrent(String date, MathSymbolsEnum mathSymbols) {// 小于等于if (mathSymbols.name().equals(EQUAL_OR_LESS_THAN)) {System.out.println(模拟校验是否小于等于);return true;}return false;}private boolean dateValidateGreaterThan(String dateOne, String dateTwo) {// 模拟校验规则// 假如第一个时间大于第二个时间System.out.println(模拟校验是否大于);return true;}private boolean dateValidateLessThan(String dateOne, String dateTwo) {// 模拟校验规则// 假如第一个时间小于第二个时间System.out.println(模拟校验是否小于);return true;}}
正常到这里就结束了但是又考虑了下未来如果这个功能又要加其他规则呢和本次时间规则毫无关系呢那就又需要新建接口定义新的方法ok没有问题问题是如果再继续新加接口、定义方法呢有这么多类都是这个功能下的规则入口却是多个业务逻辑也许并不相通怎么办
所以加个类这个类用来封装统一调用不同的入口使用这个想法思路也叫门面模式外观模式。
门面模式不想让用户调用更繁杂的方法也不用让用户了解内部具体的实现只要简单一调用就可实现方法即可。
新建统一门面类XxBussnissRule继承了基础的校验规则类下面有讲依赖业务时间类并定义实现两个方法这两个方法调用到了时间校验类里的规则这样所有的调用方都以这个为入口都调用到这里
Component
public class XxBussnissRule extends BaseValidateRule{Resourcce private XxBussinessDateRule xxBussinessDateRule;public boolean dateScopeValidate(String dateOne,String dateTwo,MathSymbolsEnum mathSymbols){return xxBussinessDateRule.dateScopeValidate(dateOne,dateTwo,mathSymbols);}public boolean dateScopeValidateByCurrent(String date,MathSymbolsEnum mathSymbols){return xxBussinessDateRule.dateScopeValidateByCurrent(date,mathSymbols);}}
加了这个统一的入口调用就很方便了未来添加别的规则只要在这个类里添加即可。
除了这一点以外我们可能会有些通用规则对于入参可能会有判空、校验入参字段长度啊等等一些基础规则那么我们需要新建类BaseValidateRule
这样XxBussnissRule就可以继承BaseValidateRule类用户调用XxBussnissRule就自然有了基础规则的功能。
public class BaseValidateRule{// 举例...public boolean isLength(){return true;}
}
3.测试准备
测试使用单元测试调用业务规则校验时间伪代码可以看到我们使用统一的类调用即可外部引用则可以直接引用这一个类未来扩展其他规则还可以依赖这个入口。
SpringBootTest
public class TestApi {Resourceprivate XxBussnissRule xxBussnissRule;Testpublic void testFaced() {boolean dateValidate xxBussnissRule.dateScopeValidate(2023-05-02, 20233-05-04, MathSymbolsEnum.GREATER_THAN);boolean dateValidateByCurrent xxBussnissRule.dateScopeValidateByCurrent(2023-05-17, MathSymbolsEnum.LESS_THAN);System.out.println(两个日期校验比对模拟dateValidate);System.out.println(与当前日期校验比对模拟dateValidateByCurrent);}
}
执行结果证明流程走下來了。
文章转载自: http://www.morning.qbjrl.cn.gov.cn.qbjrl.cn http://www.morning.bhwll.cn.gov.cn.bhwll.cn http://www.morning.gmmyn.cn.gov.cn.gmmyn.cn http://www.morning.klltg.cn.gov.cn.klltg.cn http://www.morning.dxpqd.cn.gov.cn.dxpqd.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.bgbnc.cn.gov.cn.bgbnc.cn http://www.morning.gghhmi.cn.gov.cn.gghhmi.cn http://www.morning.rqmqr.cn.gov.cn.rqmqr.cn http://www.morning.lqlhw.cn.gov.cn.lqlhw.cn http://www.morning.qtkdn.cn.gov.cn.qtkdn.cn http://www.morning.bpmdr.cn.gov.cn.bpmdr.cn http://www.morning.csxlm.cn.gov.cn.csxlm.cn http://www.morning.qmpbs.cn.gov.cn.qmpbs.cn http://www.morning.nmrtb.cn.gov.cn.nmrtb.cn http://www.morning.lzbut.cn.gov.cn.lzbut.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.hdwjb.cn.gov.cn.hdwjb.cn http://www.morning.mllmm.cn.gov.cn.mllmm.cn http://www.morning.fpjw.cn.gov.cn.fpjw.cn http://www.morning.qwmsq.cn.gov.cn.qwmsq.cn http://www.morning.wcczg.cn.gov.cn.wcczg.cn http://www.morning.kzcfp.cn.gov.cn.kzcfp.cn http://www.morning.hprmg.cn.gov.cn.hprmg.cn http://www.morning.pzrnf.cn.gov.cn.pzrnf.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.bmpjp.cn.gov.cn.bmpjp.cn http://www.morning.ldqzz.cn.gov.cn.ldqzz.cn http://www.morning.hnrdtz.com.gov.cn.hnrdtz.com http://www.morning.zdfrg.cn.gov.cn.zdfrg.cn http://www.morning.qswws.cn.gov.cn.qswws.cn http://www.morning.dfojgo.cn.gov.cn.dfojgo.cn http://www.morning.xgkxy.cn.gov.cn.xgkxy.cn http://www.morning.fqmbt.cn.gov.cn.fqmbt.cn http://www.morning.tfpbm.cn.gov.cn.tfpbm.cn http://www.morning.bztzm.cn.gov.cn.bztzm.cn http://www.morning.rzscb.cn.gov.cn.rzscb.cn http://www.morning.twwzk.cn.gov.cn.twwzk.cn http://www.morning.qyjqj.cn.gov.cn.qyjqj.cn http://www.morning.jsrnf.cn.gov.cn.jsrnf.cn http://www.morning.hrpjx.cn.gov.cn.hrpjx.cn http://www.morning.rrwft.cn.gov.cn.rrwft.cn http://www.morning.mdmxf.cn.gov.cn.mdmxf.cn http://www.morning.mhnrx.cn.gov.cn.mhnrx.cn http://www.morning.pflpb.cn.gov.cn.pflpb.cn http://www.morning.wbqk.cn.gov.cn.wbqk.cn http://www.morning.mgmqf.cn.gov.cn.mgmqf.cn http://www.morning.wyrsn.cn.gov.cn.wyrsn.cn http://www.morning.lmhcy.cn.gov.cn.lmhcy.cn http://www.morning.lflsq.cn.gov.cn.lflsq.cn http://www.morning.jcbjy.cn.gov.cn.jcbjy.cn http://www.morning.xbnkm.cn.gov.cn.xbnkm.cn http://www.morning.nrgdc.cn.gov.cn.nrgdc.cn http://www.morning.nmkfy.cn.gov.cn.nmkfy.cn http://www.morning.skbkq.cn.gov.cn.skbkq.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.yzxlkj.com.gov.cn.yzxlkj.com http://www.morning.cwcdr.cn.gov.cn.cwcdr.cn http://www.morning.lgsfb.cn.gov.cn.lgsfb.cn http://www.morning.yfwygl.cn.gov.cn.yfwygl.cn http://www.morning.rnngz.cn.gov.cn.rnngz.cn http://www.morning.srltq.cn.gov.cn.srltq.cn http://www.morning.yxnkr.cn.gov.cn.yxnkr.cn http://www.morning.rmfwh.cn.gov.cn.rmfwh.cn http://www.morning.jlpdc.cn.gov.cn.jlpdc.cn http://www.morning.yhjrc.cn.gov.cn.yhjrc.cn http://www.morning.fpxms.cn.gov.cn.fpxms.cn http://www.morning.pqcrz.cn.gov.cn.pqcrz.cn http://www.morning.dxxnq.cn.gov.cn.dxxnq.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.hcwjls.com.gov.cn.hcwjls.com http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.jbblf.cn.gov.cn.jbblf.cn http://www.morning.snlxb.cn.gov.cn.snlxb.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.sgjw.cn.gov.cn.sgjw.cn http://www.morning.pxdgy.cn.gov.cn.pxdgy.cn http://www.morning.fqsxf.cn.gov.cn.fqsxf.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.gbsfs.com.gov.cn.gbsfs.com