当前位置: 首页 > news >正文 携程网站建设评价专业网络营销 news 2025/11/6 10:18:38 携程网站建设评价,专业网络营销,网站域名到期怎么办,腾讯云域名注册官网应用场景 实现规则匹配的逻辑 比如 ,同时支持 and or 多个条件组合 新增一个条件就增加一个实现类 说明 对于这种需要实现规则匹配的逻辑#xff0c;可以考虑使用策略模式。策略模式可以将不同的算法封装成不同的策略类#xff0c;让它们可以相互替换#xff0c;…应用场景 实现规则匹配的逻辑 比如 ,同时支持 and or 多个条件组合 新增一个条件就增加一个实现类 说明 对于这种需要实现规则匹配的逻辑可以考虑使用策略模式。策略模式可以将不同的算法封装成不同的策略类让它们可以相互替换从而实现不同的行为。在这个问题中可以将不同的匹配规则封装成不同的策略类让它们可以相互替换从而实现不同的匹配行为。 1、具体实现时可以定义一个接口例如 MatchStrategy其中包含一个 match 方法用于判断给定的值是否满足当前规则。然后针对不同的规则可以实现不同的策略类例如 GreaterThanStrategy、LessThanStrategy、EqualStrategy 等它们分别实现 MatchStrategy 接口并实现自己的 match 方法。 2、对于多个条件组合的情况可以使用组合模式来实现。例如可以定义一个 CompositeMatchStrategy 类它实现了 MatchStrategy 接口并包含一个 List 类型的成员变量用于存储多个子策略。然后可以定义一个 AndMatchStrategy 类和一个 OrMatchStrategy 类它们都是 CompositeMatchStrategy 的子类分别实现了 “and” 和 “or” 运算符的匹配逻辑。 这样当需要新增一个条件时只需要新增一个实现了 MatchStrategy 接口的策略类并将它加入到 CompositeMatchStrategy 中即可。整个匹配逻辑的实现就变得非常灵活和可扩展了。 实现 假设我们需要实现一个匹配规则的功能支持以下几种规则 大于等于某个值小于等于某个值等于某个值大于某个值小于某个值 同时还需要支持 “and” 和 “or” 运算符让用户可以将多个规则组合起来使用。 1、首先顶定义一个MatchStrategy 接口用于表示匹配规则 /*** author lsh* date 2023/6/9*/ public interface MatchStrategy {boolean match(String value); } 2、然后我们针对不同的规则实现不同的策略类。例如对于大于某个值的规则我们可以实现一个 MatchStrategyImpl1 类 /*** 匹配规则 大于0*/ public class MatchStrategyImpl1 implements MatchStrategy{Overridepublic boolean match(Integer value) {return value0;} } 类似地我们还可以实现其他的策略类这些类都实现了 MatchStrategy 接口并实现了自己的 match 方法。 3、接下来我们实现组合模式。我们定义一个 CompositeMatchStrategy 类用于表示多个匹配规则的组合 /*** 组合匹配规则*/ public abstract class CompositeMatchStrategy implements MatchStrategy {protected ListMatchStrategy strategies new ArrayList();public void addStrategy(MatchStrategy strategy) {strategies.add(strategy);}}这个类是一个抽象类包含一个 List 类型的成员变量 strategies用于存储多个子策略。它还包含一个 addStrategy 方法用于向 strategies 中添加子策略。 4、针对 “and” 和 “or” 运算符分别实现 AndMatchStrategy 和 OrMatchStrategy 类它们都是 CompositeMatchStrategy 的子类 /*** 或 组合规则*/ public class OrMatchStrategy extends CompositeMatchStrategy {Overridepublic boolean match(Integer value) {for (MatchStrategy strategy : strategies) {if (strategy.match(value)) {return true;}}return false;} }/*** 且 组合规则*/ public class AndMatchStrategy extends CompositeMatchStrategy {Overridepublic boolean match(Integer value) {for (MatchStrategy strategy : strategies) {if (!strategy.match(value)) {return false;}}return true;} } 5、测试 我们期望匹配大于零并且小于3 CompositeMatchStrategy strategy new AndMatchStrategy();strategy.addStrategy(new MatchStrategyImpl1());strategy.addStrategy(new MatchStrategyImpl2(3));if(strategy.match(1)){//匹配成功log.info(1--------------------------------匹配成功);}else{log.info(1--------------------------------匹配失败);}if(strategy.match(4)){//匹配成功log.info(4--------------------------------匹配成功);}else{log.info(4--------------------------------匹配失败);}这样我们就可以灵活地组合多个规则并实现复杂的匹配逻辑了。 文章转载自: http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.lhgqc.cn.gov.cn.lhgqc.cn http://www.morning.knmp.cn.gov.cn.knmp.cn http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.lyjwb.cn.gov.cn.lyjwb.cn http://www.morning.ydxx123.cn.gov.cn.ydxx123.cn http://www.morning.xqltq.cn.gov.cn.xqltq.cn http://www.morning.brzlp.cn.gov.cn.brzlp.cn http://www.morning.bkkgt.cn.gov.cn.bkkgt.cn http://www.morning.zxfr.cn.gov.cn.zxfr.cn http://www.morning.prsxj.cn.gov.cn.prsxj.cn http://www.morning.hrypl.cn.gov.cn.hrypl.cn http://www.morning.sqfnx.cn.gov.cn.sqfnx.cn http://www.morning.zfcfx.cn.gov.cn.zfcfx.cn http://www.morning.zlnkq.cn.gov.cn.zlnkq.cn http://www.morning.nqmwk.cn.gov.cn.nqmwk.cn http://www.morning.brlcj.cn.gov.cn.brlcj.cn http://www.morning.chbcj.cn.gov.cn.chbcj.cn http://www.morning.cwpny.cn.gov.cn.cwpny.cn http://www.morning.dwwlg.cn.gov.cn.dwwlg.cn http://www.morning.xscpq.cn.gov.cn.xscpq.cn http://www.morning.dtnjr.cn.gov.cn.dtnjr.cn http://www.morning.wcyr.cn.gov.cn.wcyr.cn http://www.morning.flchj.cn.gov.cn.flchj.cn http://www.morning.nzzws.cn.gov.cn.nzzws.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.bfybb.cn.gov.cn.bfybb.cn http://www.morning.smtrp.cn.gov.cn.smtrp.cn http://www.morning.tcpnp.cn.gov.cn.tcpnp.cn http://www.morning.jxfmn.cn.gov.cn.jxfmn.cn http://www.morning.cywf.cn.gov.cn.cywf.cn http://www.morning.lmqfq.cn.gov.cn.lmqfq.cn http://www.morning.fkmqg.cn.gov.cn.fkmqg.cn http://www.morning.cwznh.cn.gov.cn.cwznh.cn http://www.morning.hdzty.cn.gov.cn.hdzty.cn http://www.morning.lwmzp.cn.gov.cn.lwmzp.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.cbnjt.cn.gov.cn.cbnjt.cn http://www.morning.byywt.cn.gov.cn.byywt.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.tbkqs.cn.gov.cn.tbkqs.cn http://www.morning.xgjhy.cn.gov.cn.xgjhy.cn http://www.morning.krfpj.cn.gov.cn.krfpj.cn http://www.morning.srbl.cn.gov.cn.srbl.cn http://www.morning.yrbqy.cn.gov.cn.yrbqy.cn http://www.morning.mjwnc.cn.gov.cn.mjwnc.cn http://www.morning.yjmlg.cn.gov.cn.yjmlg.cn http://www.morning.xhlht.cn.gov.cn.xhlht.cn http://www.morning.snktp.cn.gov.cn.snktp.cn http://www.morning.qkxt.cn.gov.cn.qkxt.cn http://www.morning.jtmrx.cn.gov.cn.jtmrx.cn http://www.morning.xldpm.cn.gov.cn.xldpm.cn http://www.morning.beeice.com.gov.cn.beeice.com http://www.morning.bssjp.cn.gov.cn.bssjp.cn http://www.morning.wanjia-sd.com.gov.cn.wanjia-sd.com http://www.morning.jypsm.cn.gov.cn.jypsm.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.trtxt.cn.gov.cn.trtxt.cn http://www.morning.btlsb.cn.gov.cn.btlsb.cn http://www.morning.dqrpz.cn.gov.cn.dqrpz.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.xqcbz.cn.gov.cn.xqcbz.cn http://www.morning.gcspr.cn.gov.cn.gcspr.cn http://www.morning.kwnbd.cn.gov.cn.kwnbd.cn http://www.morning.mhmdx.cn.gov.cn.mhmdx.cn http://www.morning.kaylyea.com.gov.cn.kaylyea.com http://www.morning.wynqg.cn.gov.cn.wynqg.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.ktmpw.cn.gov.cn.ktmpw.cn http://www.morning.kxsnp.cn.gov.cn.kxsnp.cn http://www.morning.ynrzf.cn.gov.cn.ynrzf.cn http://www.morning.swimstaracademy.cn.gov.cn.swimstaracademy.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.xrwsg.cn.gov.cn.xrwsg.cn http://www.morning.lkthj.cn.gov.cn.lkthj.cn http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn http://www.morning.yqpck.cn.gov.cn.yqpck.cn http://www.morning.cxtbh.cn.gov.cn.cxtbh.cn 查看全文 http://www.tj-hxxt.cn/news/282000.html 相关文章: 神木网站建设毕业设计都是做网站吗 相亲网站上做绿叶的女人很多做pc端网站怎么样 高端品牌网站建设公司设计网页推荐 php做网站python做什么django个人博客网站开发部署源码 做的网站打开慢优客逸家网站源码 眉山招聘网站建设wordpress 安全防护 网站添加ico5g天天奭5g天天运动网站代码 免费推广网站58it培训机构招生 国内重大新闻2021百度seo 太原公司网站建设海外推广专员 php c2c网站开发的 书扬州高端网站建设 中企动力做过的网站自己做网站用花钱么 搜索引擎网站排行榜重庆装修公司排行榜一览表 二级网站建设管理制度微信小程序开通支付功能 凡科建站多少钱wordpress 网址 显示 视频网站开发的难点wordpress 消息推送 网站做网站做任务没学历最吃香的职业 网站出现死链怎么办网站底部设计源码 网站建设合作合同模板下载绿色资源网 建设网站流程图搜索引擎seo优化 湖南高端网站制作公司合肥房产备案网 数据处理网站开发公司网站用什么系统 网站建设捌金手指下拉十四梁园区官方网站 网站排名优化软件想给公司做网站怎么做 盐城 网站开发拍卖网站怎么做 网站被挂黑链怎么办石家庄网站建设q.479185700棒 我市精神文明建设的门户网站河北搭建营销型网站 小型网站设计及建设论文范本创业做网站失败 都兰县公司网站建设淘宝关键词搜索量查询工具 python可以做网站吗免费网站建设有哪些