wloe2gf 发表于 2024-10-4 18:03:08

PHP 责任链模式的用法


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/gxE7Ps1HpANFPMYsGwzejnic9XBmP5UHtVO8c6yAIq5ibib0ib0TouDXI66Of196SGzkuQQ5YQh9ozuDARz5Zk7dwQ/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" style="width: 50%; margin-bottom: 20px;"></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>`if`语句的<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;">1. 责任链模式<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>`if`语句的<span style="color: black;">要求</span>判断是一种集中式的处理方式,<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;">2. 责任链模式<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>`if`语句的<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></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">3. 责任链模式支持职责的分离和解耦,<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>`if`语句的<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></p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">4. 责任链模式适用于处理<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>`if`语句的<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></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>`if`语句的<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></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;"><span style="color: black;">&lt;?php</span></span><span style="color: black;"><span style="color: black;">// 抽象处理器类</span></span><span style="color: black;"><span style="color: black;">abstract</span> <span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">Handler</span></span></span><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">protected</span> $nextHandler;</span><span style="color: black;"> <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">setNext</span><span style="color: black;">(Handler $handler)</span></span></span><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">$this</span>-&gt;nextHandler = $handler;</span><span style="color: black;"> <span style="color: black;">return</span>$handler;</span><span style="color: black;"> }</span><span style="color: black;"> <span style="color: black;">public</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">handle</span><span style="color: black;">($request)</span></span></span><span style="color: black;">{</span><span style="color: black;"> $result = <span style="color: black;">$this</span>-&gt;process($request);</span><span style="color: black;"> <span style="color: black;">if</span> ($result === <span style="color: black;">null</span> &amp;&amp; <span style="color: black;">$this</span>-&gt;nextHandler) {</span><span style="color: black;"> $result = <span style="color: black;">$this</span>-&gt;nextHandler-&gt;handle($request);</span><span style="color: black;"> }</span><span style="color: black;"> <span style="color: black;">return</span> $result;</span><span style="color: black;"> }</span><span style="color: black;"> <span style="color: black;">abstract</span> <span style="color: black;">protected</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">process</span><span style="color: black;">($request)</span></span>;</span><span style="color: black;">}</span><span style="color: black;"><span style="color: black;">// <span style="color: black;">详细</span>处理器类 1</span></span><span style="color: black;"><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">ConcreteHandler1</span> <span style="color: black;">extends</span> <span style="color: black;">Handler</span></span></span><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">protected</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">process</span><span style="color: black;">($request)</span></span></span><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">if</span> ($request &gt;= <span style="color: black;">0</span> &amp;&amp; $request &lt;= <span style="color: black;">10</span>) {</span><span style="color: black;"> <span style="color: black;">return</span> <span style="color: black;">ConcreteHandler1 handled the request.</span>;</span><span style="color: black;"> }</span><span style="color: black;"> <span style="color: black;">return</span> <span style="color: black;">null</span>;</span><span style="color: black;"> }</span><span style="color: black;">}</span><span style="color: black;"><span style="color: black;">// <span style="color: black;">详细</span>处理器类 2</span></span><span style="color: black;"><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">ConcreteHandler2</span> <span style="color: black;">extends</span> <span style="color: black;">Handler</span></span></span><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">protected</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">process</span><span style="color: black;">($request)</span></span></span><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">if</span> ($request &gt; <span style="color: black;">10</span> &amp;&amp; $request &lt;= <span style="color: black;">20</span>) {</span><span style="color: black;"> <span style="color: black;">return</span> <span style="color: black;">ConcreteHandler2 handled the request.</span>;</span><span style="color: black;"> }</span><span style="color: black;"> <span style="color: black;">return</span> <span style="color: black;">null</span>;</span><span style="color: black;"> }</span><span style="color: black;">}</span><span style="color: black;"><span style="color: black;">// <span style="color: black;">详细</span>处理器类 3</span></span><span style="color: black;"><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">ConcreteHandler3</span> <span style="color: black;">extends</span> <span style="color: black;">Handler</span></span></span><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">protected</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">process</span><span style="color: black;">($request)</span></span></span><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">if</span> ($request &gt; <span style="color: black;">20</span> &amp;&amp; $request &lt;= <span style="color: black;">30</span>) {</span><span style="color: black;"> <span style="color: black;">return</span> <span style="color: black;">ConcreteHandler3 handled the request.</span>;</span><span style="color: black;"> }</span><span style="color: black;"> <span style="color: black;">return</span> <span style="color: black;">null</span>;</span><span style="color: black;"> }</span><span style="color: black;">}</span><span style="color: black;"><span style="color: black;">// <span style="color: black;">详细</span>处理器类 4</span></span><span style="color: black;"><span style="color: black;"><span style="color: black;">class</span> <span style="color: black;">ConcreteHandler4</span> <span style="color: black;">extends</span> <span style="color: black;">Handler</span></span></span><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">protected</span> <span style="color: black;"><span style="color: black;">function</span> <span style="color: black;">process</span><span style="color: black;">($request)</span></span></span><span style="color: black;">{</span><span style="color: black;"> <span style="color: black;">if</span> ($request &gt; <span style="color: black;">30</span> &amp;&amp; $request &lt;= <span style="color: black;">40</span>) {</span><span style="color: black;"> <span style="color: black;">return</span> <span style="color: black;">ConcreteHandler4 handled the request.</span>;</span><span style="color: black;">}</span><span style="color: black;"> <span style="color: black;">return</span> <span style="color: black;">null</span>;</span><span style="color: black;"> }</span><span style="color: black;">}</span><span style="color: black;"><span style="color: black;">// 客户端代码</span></span><span style="color: black;">$handler1 = <span style="color: black;">new</span> ConcreteHandler1();</span><span style="color: black;">$handler2 = <span style="color: black;">new</span> ConcreteHandler2();</span><span style="color: black;">$handler3 = <span style="color: black;">new</span> ConcreteHandler3();</span><span style="color: black;">$handler4 = <span style="color: black;">new</span>ConcreteHandler4();</span><span style="color: black;">$handler1-&gt;setNext($handler2)</span><span style="color: black;"> -&gt;setNext($handler3)</span><span style="color: black;"> -&gt;setNext($handler4);</span><span style="color: black;">$requests = [<span style="color: black;">5</span>, <span style="color: black;">15</span>, <span style="color: black;">25</span>, <span style="color: black;">35</span>, <span style="color: black;">45</span>];</span><span style="color: black;"><span style="color: black;">foreach</span> ($requests <span style="color: black;">as</span> $request) {</span><span style="color: black;">$result = $handler1-&gt;handle($request);</span><span style="color: black;"> <span style="color: black;">if</span> ($result) {</span><span style="color: black;"> <span style="color: black;">echo</span> $result . <span style="color: black;">"\n"</span>;</span><span style="color: black;"> } <span style="color: black;">else</span> {</span><span style="color: black;"> <span style="color: black;">echo</span> <span style="color: black;">"No handler found for request: $request\n"</span>;</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><span style="color: black;">首要</span>定义了一个抽象的处理器类 Handler,其中<span style="color: black;">包括</span>了一个指向下一个处理器的引用。<span style="color: black;">而后</span>,<span style="color: black;">咱们</span>定义了<span style="color: black;">详细</span>的处理器类 ConcreteHandler1、ConcreteHandler2 和 ConcreteHandler3等,它们分别处理<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>
    <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> setNext()&nbsp;<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>的请求调用 handle()&nbsp;<span style="color: black;">办法</span>来测试责任链的工作<span style="color: black;">状况</span>,并输出处理结果。</p><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>进行扩展和修改。





m5k1umn 发表于 2024-10-16 21:33:06

谷歌外链发布 http://www.fok120.com/

qzmjef 发表于 2024-11-2 11:55:55

你的见解真是独到,让我受益匪浅。
页: [1]
查看完整版本: PHP 责任链模式的用法