同事写了一个责任链模式,bug无数!
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">点击上方“</span><span style="color: black;">芋道源码</span><span style="color: black;">”,<span style="color: black;">选取</span>“<a style="color: black;">设为星标</a>”</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">管她前浪,还是后浪?</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">能浪的浪,才是好浪!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">每日</span> </span><span style="color: black;"><strong style="color: blue;">10:33</strong></span><span style="color: black;"> 更新<span style="color: black;">文案</span>,<span style="color: black;">每日</span>掉亿点点头发...</span></p><span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">源码精品专栏</p>
</span> <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">原创 | Java 2021 超神之路,很肝~</span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">中文<span style="color: black;">仔细</span>注释的开源项目</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">RPC 框架 Dubbo 源码解析</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">网络应用框架 Netty 源码解析</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;">信息</span>中间件 RocketMQ</a> 源码解析</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">数据库中间件 Sharding-JDBC 和 MyCAT 源码解析</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">作业调度中间件 Elastic-Job 源码解析</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">分布式事务中间件 TCC-Transaction 源码解析</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">Eureka 和 Hystrix 源码解析</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;">Java 并发源码</a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;"><span style="color: black;">源自</span>:网络</span></a></p><a style="color: black;">什么是责任链</a><a style="color: black;">场景</a><a style="color: black;">反例</a><a style="color: black;">初步改造</a><a style="color: black;">缺点</a><a style="color: black;">责任链改造</a><a style="color: black;">责任链</a>工厂改造<a style="color: black;">聊聊其他</a>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">近期</span>,我让团队内一位成员写了一个导入功能。他<span style="color: black;">运用</span>了责任链模式,代码堆的非常多,bug <span style="color: black;">亦</span>多,<span style="color: black;">无</span>达到我预期的效果。<span style="color: black;">实质</span>上,针对导入功能,我认为模版<span style="color: black;">办法</span>更合适!为此,隔壁团队<span style="color: black;">亦</span>拿出<span style="color: black;">咱们</span>的案例,进行了集体 code review。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">学好设计模式,且不要为了练习,强行<span style="color: black;">运用</span>!让<span style="color: black;">本来</span>100行就能实现的功能,写了 3000 行!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">对错暂且不论,<span style="color: black;">咱们</span>先<span style="color: black;">一块</span><span style="color: black;">瞧瞧</span>责任链设计模式吧!</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><a style="color: black;">什么是责任链</a></span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;">「责任链模式」</strong> 是一种<span style="color: black;">行径</span>设计模式, <span style="color: black;">准许</span>你将请求沿着处理者链进行发送。收到请求后, <span style="color: black;">每一个</span>处理者均可对请求进行处理, 或将其传递给链上的下个处理者。</p><a style="color: black;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/JdLkEI9sZffA8Gu0uJuGKsmTXCXWo2iajXOialUYf1GQUGauCTibVek1DkauGUKvuAouDpd4WUZFuVuDMa6JPVbEQ/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></a>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 用户小程序,支持 RBAC 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">项目<span style="color: black;">位置</span>:https://github.com/YunaiV/ruoyi-vue-pro</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><a style="color: black;">场景</a></span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">责任链的<span style="color: black;">运用</span>场景还是比较多的</p>多<span style="color: black;">要求</span>流程判断:权限<span style="color: black;">掌控</span>ERP 系统流程审批:总经理、人事经理、项目经理Java 过滤器 的底层实现 Filter<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">倘若</span>不<span style="color: black;">运用</span>该设计模式,<span style="color: black;">那样</span>当需求有所改变时,就会使得代码臃肿<span style="color: black;">或</span>难以<span style="color: black;">守护</span>,例如下面的例子</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><a style="color: black;">反例</a></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">假设<span style="color: black;">此刻</span>有一个闯关游戏,进入下一关的<span style="color: black;">要求</span>是上一关的分数要高于xx</p>游戏一共 3 个关卡进入第二关<span style="color: black;">必须</span><span style="color: black;">第1</span>关的游戏得分大于等于 80进入第三关<span style="color: black;">必须</span>第二关的游戏得分大于等于 90<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span>代码<span style="color: black;">能够</span><span style="color: black;">这般</span>写</p><span style="color: black;">//<span style="color: black;">第1</span>关</span><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">FirstPassHandler</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">int</span> <span style="color: black;">handler</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> System.out.println(<span style="color: black;">"<span style="color: black;">第1</span>关-->FirstPassHandler"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">return</span> <span style="color: black;">80</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//第二关</span><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">SecondPassHandler</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">int</span> <span style="color: black;">handler</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> System.out.println(<span style="color: black;">"第二关-->SecondPassHandler"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">return</span> <span style="color: black;">90</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//第三关</span><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">ThirdPassHandler</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">int</span> <span style="color: black;">handler</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> System.out.println(<span style="color: black;">"第三关-->ThirdPassHandler,这是最后一关啦"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">return</span> <span style="color: black;">95</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//客户端</span><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">HandlerClient</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span><span style="color: black;">(String[] args)</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>FirstPassHandler firstPassHandler =<span style="color: black;">new</span> FirstPassHandler();<span style="color: black;">//<span style="color: black;">第1</span>关</span> SecondPassHandler secondPassHandler = <span style="color: black;">new</span> SecondPassHandler();<span style="color: black;">//第二关</span> ThirdPassHandler thirdPassHandler = <span style="color: black;">new</span>ThirdPassHandler();<span style="color: black;">//第三关</span> <span style="color: black;">int</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> firstScore = firstPassHandler.handler();</p> <span style="color: black;">//<span style="color: black;">第1</span>关的分数大于等于80则进入第二关</span> <span style="color: black;">if</span>(firstScore >= <span style="color: black;">80</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">int</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">secondScore = secondPassHandler.handler();</p> <span style="color: black;">//第二关的分数大于等于90则进入第二关</span> <span style="color: black;">if</span>(secondScore >= <span style="color: black;">90</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> thirdPassHandler.handler();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">那样</span><span style="color: black;">倘若</span>这个游戏有100关,<span style="color: black;">咱们</span>的代码很可能就会写成这个样子</p><span style="color: black;">if</span>(第<span style="color: black;">1</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">关<span style="color: black;">经过</span>){</p> <span style="color: black;">// 第2关 游戏</span> <span style="color: black;">if</span>(第<span style="color: black;">2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">关<span style="color: black;">经过</span>){</p> <span style="color: black;">// 第3关 游戏</span> <span style="color: black;">if</span>(第<span style="color: black;">3</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">关<span style="color: black;">经过</span>){</p> <span style="color: black;">// 第4关 游戏</span> <span style="color: black;">if</span>(第<span style="color: black;">4</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">关<span style="color: black;">经过</span>){</p> <span style="color: black;">// 第5关 游戏</span> <span style="color: black;">if</span>(第<span style="color: black;">5</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">关<span style="color: black;">经过</span>){</p> <span style="color: black;">// 第6关 游戏</span> <span style="color: black;">if</span>(第<span style="color: black;">6</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">关<span style="color: black;">经过</span>){</p> <span style="color: black;">//...</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> } </p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这种代码不仅冗余,并且当<span style="color: black;">咱们</span>要将某两关进行<span style="color: black;">调节</span>时会对代码非常大的改动,这种操作的<span style="color: black;">危害</span>是很高的,<span style="color: black;">因此呢</span>,该写法非常糟糕</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><a style="color: black;">初步改造</a></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">怎样</span><span style="color: black;">处理</span>这个问题,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">经过</span>链表将每一关连接起来,形成责任链的方式,<span style="color: black;">第1</span>关<span style="color: black;">经过</span>后是第二关,第二关<span style="color: black;">经过</span>后是第三关 ....,<span style="color: black;">这般</span>客户端就不<span style="color: black;">必须</span>进行多重 if 的判断了</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">FirstPassHandler</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> * <span style="color: black;">第1</span>关的下一关是 第二关</p> */
</span> <span style="color: black;">private</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">SecondPassHandler secondPassHandler;</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">void</span> <span style="color: black;">setSecondPassHandler</span><span style="color: black;">(SecondPassHandler secondPassHandler)</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.secondPassHandler = secondPassHandler;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">//本关卡游戏得分</span> <span style="color: black;"><span style="color: black;">private</span> <span style="color: black;">int</span> <span style="color: black;">play</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">return</span> <span style="color: black;">80</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">int</span> <span style="color: black;">handler</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> System.out.println(<span style="color: black;">"<span style="color: black;">第1</span>关-->FirstPassHandler"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">if</span>(play() >= <span style="color: black;">80</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">//分数>=80 并且存在下一关才进入下一关</span> <span style="color: black;">if</span>(<span style="color: black;">this</span>.secondPassHandler != <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">return</span> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.secondPassHandler.handler();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">return</span> <span style="color: black;">80</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">SecondPassHandler</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> * 第二关的下一关是 第三关</p> */
</span> <span style="color: black;">private</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ThirdPassHandler thirdPassHandler;</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">void</span> <span style="color: black;">setThirdPassHandler</span><span style="color: black;">(ThirdPassHandler thirdPassHandler)</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.thirdPassHandler = thirdPassHandler;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">//本关卡游戏得分</span> <span style="color: black;"><span style="color: black;">private</span> <span style="color: black;">int</span> <span style="color: black;">play</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">return</span> <span style="color: black;">90</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">int</span> <span style="color: black;">handler</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>System.out.println(<span style="color: black;">"第二关-->SecondPassHandler"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">if</span>(play() >= <span style="color: black;">90</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">//分数>=90 并且存在下一关才进入下一关</span> <span style="color: black;">if</span>(<span style="color: black;">this</span>.thirdPassHandler != <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">return</span> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.thirdPassHandler.handler();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">return</span> <span style="color: black;">90</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">ThirdPassHandler</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">//本关卡游戏得分</span> <span style="color: black;"><span style="color: black;">private</span> <span style="color: black;">int</span> <span style="color: black;">play</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">return</span> <span style="color: black;">95</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> * 这是最后一关,<span style="color: black;">因此呢</span><span style="color: black;">无</span>下一关</p> */
</span> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">int</span> <span style="color: black;">handler</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> System.out.println(<span style="color: black;">"第三关-->ThirdPassHandler,这是最后一关啦"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> play();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">HandlerClient</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span><span style="color: black;">(String[] args)</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> FirstPassHandler firstPassHandler = <span style="color: black;">new</span> FirstPassHandler();<span style="color: black;">//第一关</span> SecondPassHandler secondPassHandler = <span style="color: black;">new</span> SecondPassHandler();<span style="color: black;">//第二关</span> ThirdPassHandler thirdPassHandler = <span style="color: black;">new</span> ThirdPassHandler();<span style="color: black;">//第三关</span>firstPassHandler.setSecondPassHandler(secondPassHandler);<span style="color: black;">//<span style="color: black;">第1</span>关的下一关是第二关</span> secondPassHandler.setThirdPassHandler(thirdPassHandler);<span style="color: black;">//第二关的下一关是第三关</span> <span style="color: black;">//说明:<span style="color: black;">由于</span>第三关是最后一关,<span style="color: black;">因此呢</span><span style="color: black;">无</span>下一关</span> <span style="color: black;">//<span style="color: black;">起始</span>调用<span style="color: black;">第1</span>关 每一个关卡<span style="color: black;">是不是</span>进入下一关卡 在<span style="color: black;">每一个</span>关卡中判断</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> firstPassHandler.handler();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><a style="color: black;">缺点</a></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">现有模式的缺点</p><span style="color: black;">每一个</span>关卡中都有下一关的成员变量并且是不<span style="color: black;">同样</span>的,形成链很不方便代码的扩展性非常<span style="color: black;">欠好</span>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><a style="color: black;">责任链改造</a></h3>既然<span style="color: black;">每一个</span>关卡中都有下一关的成员变量并且是不<span style="color: black;">同样</span>的,<span style="color: black;">那样</span><span style="color: black;">咱们</span><span style="color: black;">能够</span>在关卡上抽象出一个父类<span style="color: black;">或</span>接口,<span style="color: black;">而后</span><span style="color: black;">每一个</span><span style="color: black;">详细</span>的关卡去继承<span style="color: black;">或</span>实现<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">有了思路,<span style="color: black;">咱们</span>先来简单介绍一下责任链设计模式的<strong style="color: blue;">「基本<span style="color: black;">构成</span>」</strong></p>抽象处理者(Handler)角色:定义一个处理请求的接口,<span style="color: black;">包括</span>抽象处理<span style="color: black;">办法</span>和一个后继连接。<span style="color: black;">详细</span>处理者(Concrete Handler)角色:实现抽象处理者的处理<span style="color: black;">办法</span>,判断能否处理<span style="color: black;">这次</span>请求,<span style="color: black;">倘若</span><span style="color: black;">能够</span>处理请求则处理,否则将该请求转给它的后继者。客户类(Client)角色:创建处理链,并向链头的<span style="color: black;">详细</span>处理者对象提交请求,它不关心处理细节和请求的传递过程。<a style="color: black;"><span style="color: black;"><img src="https://mmbiz.qpic.cn/mmbiz_png/JdLkEI9sZffA8Gu0uJuGKsmTXCXWo2iajP03r8Q6IAej09bWHnLF5gG8MTd0iajbfTPkDBxkAPviaYqSCicic7cQxCA/640?wx_fmt=gif&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></span></a>责任链改造<span style="color: black;">public</span> <span style="color: black;">abstract</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">AbstractHandler</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> * 下一关用当前抽象类来接收</p> */
</span> <span style="color: black;">protected</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">AbstractHandler next;</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">void</span> <span style="color: black;">setNext</span><span style="color: black;">(AbstractHandler next)</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.next = next;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">abstract</span> <span style="color: black;">int</span> <span style="color: black;">handler</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">FirstPassHandler</span> <span style="color: black;">extends</span> <span style="color: black;">AbstractHandler</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;"><span style="color: black;">private</span> <span style="color: black;">int</span> <span style="color: black;">play</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">return</span> <span style="color: black;">80</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">@Override</span> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">int</span> <span style="color: black;">handler</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> System.out.println(<span style="color: black;">"<span style="color: black;">第1</span>关-->FirstPassHandler"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">int</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> score = play();</p> <span style="color: black;">if</span>(score >= <span style="color: black;">80</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">//分数>=80 并且存在下一关才进入下一关</span> <span style="color: black;">if</span>(<span style="color: black;">this</span>.next != <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">return</span> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.next.handler();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> score;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">SecondPassHandler</span> <span style="color: black;">extends</span> <span style="color: black;">AbstractHandler</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;"><span style="color: black;">private</span> <span style="color: black;">int</span> <span style="color: black;">play</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">return</span> <span style="color: black;">90</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">int</span> <span style="color: black;">handler</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> System.out.println(<span style="color: black;">"第二关-->SecondPassHandler"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">int</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> score = play();</p> <span style="color: black;">if</span>(score >= <span style="color: black;">90</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">//分数>=90 并且存在下一关才进入下一关</span> <span style="color: black;">if</span>(<span style="color: black;">this</span>.next != <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">return</span> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.next.handler();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> score;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">ThirdPassHandler</span> <span style="color: black;">extends</span> <span style="color: black;">AbstractHandler</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;"><span style="color: black;">private</span> <span style="color: black;">int</span> <span style="color: black;">play</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">return</span> <span style="color: black;">95</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">int</span> <span style="color: black;">handler</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> System.out.println(<span style="color: black;">"第三关-->ThirdPassHandler"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p> <span style="color: black;">int</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> score = play();</p> <span style="color: black;">if</span>(score >= <span style="color: black;">95</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">//分数>=95 并且存在下一关才进入下一关</span> <span style="color: black;">if</span>(<span style="color: black;">this</span>.next != <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">){</p> <span style="color: black;">return</span> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.next.handler();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> score;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">HandlerClient</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span><span style="color: black;">(String[] args)</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> FirstPassHandler firstPassHandler = <span style="color: black;">new</span> FirstPassHandler();<span style="color: black;">//<span style="color: black;">第1</span>关</span> SecondPassHandler secondPassHandler = <span style="color: black;">new</span>SecondPassHandler();<span style="color: black;">//第二关</span> ThirdPassHandler thirdPassHandler = <span style="color: black;">new</span> ThirdPassHandler();<span style="color: black;">//第三关</span> <span style="color: black;">// 和上面<span style="color: black;">无</span>更改的客户端代码相比,<span style="color: black;">仅有</span><span style="color: black;">这儿</span>的set<span style="color: black;">办法</span><span style="color: black;">出现</span>变化,其他都是<span style="color: black;">同样</span>的</span>firstPassHandler.setNext(secondPassHandler);<span style="color: black;">//<span style="color: black;">第1</span>关的下一关是第二关</span> secondPassHandler.setNext(thirdPassHandler);<span style="color: black;">//第二关的下一关是第三关</span> <span style="color: black;">//说明:<span style="color: black;">由于</span>第三关是最后一关,<span style="color: black;">因此呢</span><span style="color: black;">无</span>下一关</span> <span style="color: black;">//从<span style="color: black;">第1</span>个关卡<span style="color: black;">起始</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">firstPassHandler.handler();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<h3 style="color: black; text-align: left; margin-bottom: 10px;"><a style="color: black;">责任链工厂改造</a></h3>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">针对</span>上面的请求链,<span style="color: black;">咱们</span><span style="color: black;">亦</span><span style="color: black;">能够</span>把这个关系<span style="color: black;">守护</span>到配置文件中<span style="color: black;">或</span>一个枚举中。我将<span style="color: black;">运用</span>枚举来教会<span style="color: black;">大众</span>怎么动态的配置请求链并且将<span style="color: black;">每一个</span>请求者形成一条调用链。</p><span style="color: black;">public</span> <span style="color: black;">enum</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> GatewayEnum {</p> <span style="color: black;">// handlerId, 拦截者名<span style="color: black;">叫作</span>,全限定类名,preHandlerId,nextHandlerId</span> API_HANDLER(<span style="color: black;">new</span> GatewayEntity(<span style="color: black;">1</span>, <span style="color: black;">"api接口限流"</span>, <span style="color: black;">"cn.dgut.design.chain_of_responsibility.GateWay.impl.ApiLimitGatewayHandler"</span>, <span style="color: black;">null</span>, <span style="color: black;">2</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)),</p>BLACKLIST_HANDLER(<span style="color: black;">new</span> GatewayEntity(<span style="color: black;">2</span>, <span style="color: black;">"黑名单拦截"</span>, <span style="color: black;">"cn.dgut.design.chain_of_responsibility.GateWay.impl.BlacklistGatewayHandler"</span>, <span style="color: black;">1</span>, <span style="color: black;">3</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)),</p> SESSION_HANDLER(<span style="color: black;">new</span> GatewayEntity(<span style="color: black;">3</span>, <span style="color: black;">"用户会话拦截"</span>, <span style="color: black;">"cn.dgut.design.chain_of_responsibility.GateWay.impl.SessionGatewayHandler"</span>, <span style="color: black;">2</span>, <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)),</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> GatewayEntity gatewayEntity;</p> <span style="color: black;"><span style="color: black;">public</span> GatewayEntity <span style="color: black;">getGatewayEntity</span><span style="color: black;">()</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> gatewayEntity;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> GatewayEnum(GatewayEntity gatewayEntity) {</p> <span style="color: black;">this</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.gatewayEntity = gatewayEntity;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">GatewayEntity</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">private</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">String name;</p> <span style="color: black;">private</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> String conference;</p> <span style="color: black;">private</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Integer handlerId;</p> <span style="color: black;">private</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Integer preHandlerId;</p> <span style="color: black;">private</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> Integer nextHandlerId;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">interface</span> <span style="color: black;">GatewayDao</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">* <span style="color: black;">按照</span> handlerId 获取配置项</p> * <span style="color: black;">@param</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> handlerId</p> * <span style="color: black;">@return</span> */
</span> <span style="color: black;">GatewayEntity <span style="color: black;">getGatewayEntity</span><span style="color: black;">(Integer handlerId)</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p> <span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> * 获取<span style="color: black;">第1</span>个处理者</p> * <span style="color: black;">@return</span> */
</span> <span style="color: black;">GatewayEntity <span style="color: black;">getFirstGatewayEntity</span><span style="color: black;">()</span></span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">GatewayImpl</span> <span style="color: black;">implements</span> <span style="color: black;">GatewayDao</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> * 初始化,将枚举中配置的handler初始化到map中,方便获取</p> */
</span> <span style="color: black;">private</span> <span style="color: black;">static</span> Map<Integer, GatewayEntity> gatewayEntityMap = <span style="color: black;">new</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">HashMap<>();</p> <span style="color: black;">static</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> GatewayEnum[] values = GatewayEnum.values();</p> <span style="color: black;">for</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> (GatewayEnum value : values) {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> GatewayEntity gatewayEntity = value.getGatewayEntity();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">gatewayEntityMap.put(gatewayEntity.getHandlerId(), gatewayEntity);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">@Override</span> <span style="color: black;"><span style="color: black;">public</span> GatewayEntity <span style="color: black;">getGatewayEntity</span><span style="color: black;">(Integer handlerId)</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">gatewayEntityMap.get(handlerId);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">@Override</span> <span style="color: black;"><span style="color: black;">public</span> GatewayEntity <span style="color: black;">getFirstGatewayEntity</span><span style="color: black;">()</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">for</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> (Map.Entry entry : gatewayEntityMap.entrySet()) {
</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">GatewayEntity value = entry.getValue();</p> <span style="color: black;">// <span style="color: black;">无</span>上一个handler的<span style="color: black;">便是</span><span style="color: black;">第1</span>个</span> <span style="color: black;">if</span> (value.getPreHandlerId() == <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> value;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">return</span> <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">GatewayHandlerEnumFactory</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">private</span> <span style="color: black;">static</span>GatewayDao gatewayDao =<span style="color: black;">new</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> GatewayImpl();</p> <span style="color: black;">// <span style="color: black;">供给</span>静态<span style="color: black;">办法</span>,获取<span style="color: black;">第1</span>个handler</span> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> GatewayHandler <span style="color: black;">getFirstGatewayHandler</span><span style="color: black;">()</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">GatewayEntity firstGatewayEntity = gatewayDao.getFirstGatewayEntity();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> GatewayHandler firstGatewayHandler = newGatewayHandler(firstGatewayEntity);</p> <span style="color: black;">if</span> (firstGatewayHandler == <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p> <span style="color: black;">return</span> <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">GatewayEntity tempGatewayEntity = firstGatewayEntity;</p> Integer nextHandlerId = <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> GatewayHandler tempGatewayHandler = firstGatewayHandler;</p> <span style="color: black;">// 迭代遍历所有handler,以及将它们链接起来</span> <span style="color: black;">while</span> ((nextHandlerId = tempGatewayEntity.getNextHandlerId()) != <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">) {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> GatewayEntity gatewayEntity = gatewayDao.getGatewayEntity(nextHandlerId);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">GatewayHandler gatewayHandler = newGatewayHandler(gatewayEntity);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> tempGatewayHandler.setNext(gatewayHandler);</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> tempGatewayHandler = gatewayHandler;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">tempGatewayEntity = gatewayEntity;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">// 返回<span style="color: black;">第1</span>个handler</span> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> firstGatewayHandler;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> * 反射实体化<span style="color: black;">详细</span>的处理者</p> * <span style="color: black;">@param</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> firstGatewayEntity</p> * <span style="color: black;">@return</span> */
</span> <span style="color: black;"><span style="color: black;">private</span> <span style="color: black;">static</span> GatewayHandler <span style="color: black;">newGatewayHandler</span><span style="color: black;">(GatewayEntity firstGatewayEntity)</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;">// 获取全限定类名</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> String className = firstGatewayEntity.getConference(); </p> <span style="color: black;">try</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> {</p> <span style="color: black;">// <span style="color: black;">按照</span>全限定类名,加载并初始化该类,即会初始化该类的静态段</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Class clazz = Class.forName(className);
</p> <span style="color: black;">return</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> (GatewayHandler) clazz.newInstance();</p> } <span style="color: black;">catch</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(ClassNotFoundException | IllegalAccessException | InstantiationException e) {</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> e.printStackTrace();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p> <span style="color: black;">return</span> <span style="color: black;">null</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">GetewayClient</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p> <span style="color: black;"><span style="color: black;">public</span> <span style="color: black;">static</span> <span style="color: black;">void</span> <span style="color: black;">main</span><span style="color: black;">(String[] args)</span> </span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">GetewayHandler firstGetewayHandler = GetewayHandlerEnumFactory.getFirstGetewayHandler();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> firstGetewayHandler.service();</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> }</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">基于微服务的思想,构建在 B2C 电商场景下的项目实战。核心技术栈,是 Spring Boot + Dubbo 。<span style="color: black;">将来</span>,会重<span style="color: black;">形成</span> Spring Cloud Alibaba 。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">项目<span style="color: black;">位置</span>:https://github.com/YunaiV/onemall</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;"><a style="color: black;">聊聊其他</a></span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">设计模式有<span style="color: black;">非常多</span>,责任链只是其中的一种,我觉得<span style="color: black;">特别有</span>意思,非常值得一学。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">设计模式确实是一门艺术,仍需<span style="color: black;">奋斗</span>呀!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">欢迎加入我的知识星球,<span style="color: black;">一块</span>探讨架构,交流源码。加入方式,<strong style="color: blue;"><span style="color: black;">长按下方二维码噢</span></strong>
</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">已在知识星球更新源码解析如下:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><a style="color: black;"><span style="color: black;"><img src="data:image/svg+xml,%3C%3Fxml version=1.0 encoding=UTF-8%3F%3E%3Csvg width=1px height=1px viewBox=0 0 1 1 version=1.1 xmlns=http://www.w3.org/2000/svg xmlns:xlink=http://www.w3.org/1999/xlink%3E%3Ctitle%3E%3C/title%3E%3Cg stroke=none stroke-width=1 fill=none fill-rule=evenodd fill-opacity=0%3E%3Cg transform=translate(-249.000000, -126.000000) fill=%23FFFFFF%3E%3Crect x=249 y=126 width=1 height=1%3E%3C/rect%3E%3C/g%3E%3C/g%3E%3C/svg%3E" style="width: 50%; margin-bottom: 20px;"></span></a></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">近期</span>更新《芋道 SpringBoot 2.X 入门》系列</span><span style="color: black;">,<span style="color: black;">已然</span> 101 余篇,覆盖了 </span><span style="color: black;">MyBatis、Redis、MongoDB、ES、分库分表、读写分离、SpringMVC、Webflux、权限、WebSocket、Dubbo、RabbitMQ、RocketMQ、Kafka、性能测试等等内容。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">供给</span>近 3W 行<span style="color: black;">代码</span>的 SpringBoot 示例,以及超 4W 行代码的电商微服务项目。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">获取方式:点“</span><span style="color: black;"><strong style="color: blue;">在看</strong></span><span style="color: black;">”,关注公众号并回复 </span><span style="color: black;"><strong style="color: blue;">666</strong></span><span style="color: black;"> 领取,<span style="color: black;">更加多</span>内容<span style="color: black;">持续</span>奉上。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;"><span style="color: black;">文案</span>有<span style="color: black;">帮忙</span>的话,在看,转发吧。</strong></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><strong style="color: blue;">谢谢支持哟 (*^__^*)</strong></span></p>
软文发布平台 http://www.fok120.com/ i免费外链发布平台 http://www.fok120.com/ 你的努力一定会被看见,相信自己,加油。
页:
[1]