1fy07h 发表于 2024-10-4 18:01:59

PHP设计模式-责任链/策略/装饰器/适配器/桥接模式


    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">微X</span>公众号:PHP在线</p><span style="color: black;">责任链模式:</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">顾名思义,责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链。这种模式给予请求的类型,对请求的发送者和接收者进行解耦。这种类型的设计模式属于<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>对另一个接收者的引用。如果一个对象<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>,无须关心请求的处理细节和请求的传递</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例代码:</p><span style="color: black;">&lt;?php</span><span style="color: black;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>&nbsp;*&nbsp;<span style="color: black;">@authors</span>&nbsp;BaAGee&nbsp;(asm19950109<span style="color: black;">@hotmail</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.com)</p>&nbsp;*&nbsp;<span style="color: black;">@date</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;2017-09-22&nbsp;20:39:14</p>&nbsp;*/
    </span><span style="color: black;">//&nbsp;领班</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">Foreman</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;自己的等级</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">private</span>&nbsp;$level=<span style="color: black;">1</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;上级</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">protected</span>$superior=<span style="color: black;">Director</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">process</span><span style="color: black;">($level)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>(<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;level&gt;=$level){</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;自己能处理问题的级别大于等于当前事情级别,就自己处理</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">领班处理</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.PHP_EOL;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<span style="color: black;">else</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color: black;">new</span>&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;superior)-&gt;process($level);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;主管</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">Director</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;自己的等级</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">private</span>&nbsp;$level=<span style="color: black;">2</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;上级</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">protected</span>&nbsp;$superior=<span style="color: black;">Manager</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">process</span><span style="color: black;">($level)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>(<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;level&gt;=$level){</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">主管处理</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.PHP_EOL;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<span style="color: black;">else</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color: black;">new</span>&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;superior)-&gt;process($level);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;经理</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">Manager</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;自己的等级</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">private</span>&nbsp;$level=<span style="color: black;">3</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;上级</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">protected</span>&nbsp;$superior=<span style="color: black;">TopManager</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">process</span><span style="color: black;">($level)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>(<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;level&gt;=$level){</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">经理处理</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.PHP_EOL;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<span style="color: black;">else</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color: black;">new</span>&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;superior)-&gt;process($level);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;总经理</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">TopManager</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;自己的等级</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">private</span>&nbsp;$level=<span style="color: black;">4</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;上级</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">protected</span>&nbsp;$superior=<span style="color: black;">President</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">process</span><span style="color: black;">($level)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">if</span>(<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;level&gt;=$level){</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">总经理处理</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.PHP_EOL;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<span style="color: black;">else</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(<span style="color: black;">new</span>&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;superior)-&gt;process($level);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;董事长</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">President</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;自己的等级</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">private</span>&nbsp;$level=<span style="color: black;">5</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">process</span><span style="color: black;">($level)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">董事长处理</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.PHP_EOL;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;责任链模式处理问题</span>$level=rand(<span style="color: black;">1</span>,<span style="color: black;">5</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">print</span>(<span style="color: black;">问题级别:</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.$level);</p>$foreman=<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;Foreman();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$foreman-&gt;process($level);</p><span style="color: black;">策略模式:</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例代码</p><span style="color: black;">&lt;?php</span><span style="color: black;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>&nbsp;*&nbsp;<span style="color: black;">@authors</span>&nbsp;BaAGee&nbsp;(asm19950109<span style="color: black;">@hotmail</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.com)</p>&nbsp;*&nbsp;<span style="color: black;">@date</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;2017-09-22&nbsp;21:03:36</p>&nbsp;*/
    </span><span style="color: black;">//&nbsp;计算的接口</span><span style="color: black;"><span style="color: black;">interface</span>&nbsp;<span style="color: black;">Calc</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">process</span><span style="color: black;">($num1,$num2)</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;">//&nbsp;加法计算</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">AddCalc</span>&nbsp;<span style="color: black;">implements</span>&nbsp;<span style="color: black;">Calc</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">process</span><span style="color: black;">($num1,$num2)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;$num1+$num2;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;减法计算</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">SubtractCalc</span>&nbsp;<span style="color: black;">implements</span>&nbsp;<span style="color: black;">Calc</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">process</span><span style="color: black;">($num1,$num2)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;$num1-$num2;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;乘法计算</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">MultiplyCalc</span>&nbsp;<span style="color: black;">implements</span>&nbsp;<span style="color: black;">Calc</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">process</span><span style="color: black;">($num1,$num2)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$num1*$num2;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;除法计算</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">DivideCalc</span>&nbsp;<span style="color: black;">implements</span>&nbsp;<span style="color: black;">Calc</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">process</span><span style="color: black;">($num1,$num2)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;$num1/$num2;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;计算器类</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">Calculator</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;<span style="color: black;">保留</span>计算类</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">private</span>&nbsp;$calc=<span style="color: black;">null</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;<span style="color: black;">@param</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;$operator&nbsp;[计算器运算<span style="color: black;">办法</span>]</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/
    </span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">__construct</span><span style="color: black;">($operator)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$operCalc=$operator.<span style="color: black;">Calc</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$this</span>-&gt;calc=<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;$operCalc();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;计算</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">calc</span><span style="color: black;">($num1,$num2)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;calc-&gt;process($num1,$num2);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>$operators=<span style="color: black;">array</span>(<span style="color: black;">Add</span>,<span style="color: black;">Subtract</span>,<span style="color: black;">Divide</span>,<span style="color: black;">Multiply</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>$oper=$operators[rand(<span style="color: black;">0</span>,<span style="color: black;">3</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">)];</p>$calculator=<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Calculator($oper);</p>$res=$calculator-&gt;calc(<span style="color: black;">100</span>,<span style="color: black;">2</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">100&nbsp;</span>.$oper.<span style="color: black;">&nbsp;2&nbsp;res:</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.$res;</p><span style="color: black;">装饰器模式</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例代码:</p><span style="color: black;">&lt;?php</span><span style="color: black;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>&nbsp;*&nbsp;<span style="color: black;">@authors</span>&nbsp;BaAGee&nbsp;(asm19950109<span style="color: black;">@hotmail</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.com)</p>&nbsp;*&nbsp;<span style="color: black;">@date</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2017-09-22&nbsp;21:35:00</p>&nbsp;*/
    </span><span style="color: black;">//&nbsp;装饰器模式做<span style="color: black;">文案</span>修饰功能</span><span style="color: black;">//&nbsp;基本的<span style="color: black;">文案</span>类</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">BaseArticle</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;<span style="color: black;">文案</span>内容</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">protected</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;$text;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;<span style="color: black;">文案</span>对象</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">protected</span>&nbsp;$artObj=<span style="color: black;">null</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">__construct</span><span style="color: black;">($text)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;text=$text;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">decorator</span><span style="color: black;">()</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;text;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;添加<span style="color: black;">文案</span>摘要,继承<span style="color: black;">基本</span><span style="color: black;">文案</span></span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">SummaryArticle</span>&nbsp;<span style="color: black;">extends</span>&nbsp;<span style="color: black;">BaseArticle</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;传递一个<span style="color: black;">文案</span>对象</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">__construct</span><span style="color: black;">(BaseArticle&nbsp;$artObj)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;artObj=$artObj;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;decorator();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">decorator</span><span style="color: black;">()</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">$this</span>-&gt;text=<span style="color: black;">【加了摘要】</span>.<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;artObj-&gt;text;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;添加<span style="color: black;">文案</span>写作时间,继承<span style="color: black;">基本</span><span style="color: black;">文案</span></span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">TimeArticle</span>&nbsp;<span style="color: black;">extends</span>&nbsp;<span style="color: black;">BaseArticle</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">__construct</span><span style="color: black;">(BaseArticle&nbsp;$artObj)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;artObj=$artObj;</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;decorator();</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">decorator</span><span style="color: black;">()</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">$this</span>-&gt;text=<span style="color: black;">$this</span>-&gt;artObj-&gt;text.<span style="color: black;">【时间:</span>.date(<span style="color: black;">Y-m-d&nbsp;H:i:s</span>).<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;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p>$ba=<span style="color: black;">new</span>&nbsp;BaseArticle(<span style="color: black;"><span style="color: black;">文案</span>主体-<span style="color: black;">文案</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;">);</p>$sa=<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;SummaryArticle($ba);</p>$ta=<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">TimeArticle($sa);</p><span style="color: black;">echo</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;$ba-&gt;decorator().PHP_EOL;</p><span style="color: black;">echo</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;$sa-&gt;decorator().PHP_EOL;</p><span style="color: black;">echo</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;$ta-&gt;decorator().PHP_EOL;</p><span style="color: black;">适配器模式</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例代码</p><span style="color: black;">&lt;?php</span><span style="color: black;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>&nbsp;*&nbsp;<span style="color: black;">@authors</span>&nbsp;BaAGee&nbsp;(asm19950109<span style="color: black;">@hotmail</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.com)</p>&nbsp;*&nbsp;<span style="color: black;">@date</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;2017-09-22&nbsp;22:06:05</p>&nbsp;*/
    </span><span style="color: black;">//&nbsp;假设天气接口</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">Weather</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;">static</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">show</span><span style="color: black;">()</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$today=<span style="color: black;">array</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">"city"</span>=&gt;<span style="color: black;">"北京"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">"cityid"</span>=&gt;<span style="color: black;">"101010100"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">"temp1"</span>=&gt;<span style="color: black;">"24℃"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">"temp2"</span>=&gt;<span style="color: black;">"11℃"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">,</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">"weather"</span>=&gt;<span style="color: black;">"雷阵雨转多云"</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;将数据序列化,只能是php<span style="color: black;">才可</span>反序列化操作</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">/*a:5:{s:4:"city";s:6:"北京";s:6:"cityid";s:9:"101010100";s:5:"temp1";s:5:"24℃";s:5:"temp2";s:5:"11℃";s:7:"weather";s:18:"雷阵雨转多云";}城市:北京*/</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;serialize($today);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</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;">&nbsp;*&nbsp;适配器将php序列化后的数据反序列化后转化成json格式,<span style="color: black;">这般</span>其他语言就能处理了</p>&nbsp;*/
    </span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">AdapterWeather</span>&nbsp;<span style="color: black;">extends</span>&nbsp;<span style="color: black;">Weather</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;">static</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">show</span><span style="color: black;">()</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;json_encode(unserialize(<span style="color: black;">parent</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">::show()));</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;php客户端调用</span><span style="color: black;">print</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">(Weather::show());</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">$weather=unserialize(Weather::show());</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">城市:</span>.$weather[<span style="color: black;">city</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">].PHP_EOL;</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">城市ID:</span>.$weather[<span style="color: black;">city</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">].PHP_EOL;</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">最高温:</span>.$weather[<span style="color: black;">temp1</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">].PHP_EOL;</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">最低温:</span>.$weather[<span style="color: black;">temp2</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">].PHP_EOL;</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">天气:</span>.$weather[<span style="color: black;">weather</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">].PHP_EOL;</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">--------------以下假设是python<span style="color: black;">经过</span>适配器获取天气数据----------</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.PHP_EOL;</p><span style="color: black;">//&nbsp;假设python处理天气,<span style="color: black;">经过</span>适配器获取天气数据</span>$weather=json_decode(AdapterWeather::show(),<span style="color: black;">true</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">城市:</span>.$weather[<span style="color: black;">city</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">].PHP_EOL;</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">城市ID:</span>.$weather[<span style="color: black;">city</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">].PHP_EOL;</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">最高温:</span>.$weather[<span style="color: black;">temp1</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">].PHP_EOL;</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">最低温:</span>.$weather[<span style="color: black;">temp2</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">].PHP_EOL;</p><span style="color: black;">echo</span>&nbsp;<span style="color: black;">天气:</span>.$weather[<span style="color: black;">weather</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">].PHP_EOL;</p><span style="color: black;">桥接模式</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">示例代码:</p><span style="color: black;">&lt;?php</span><span style="color: black;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/**</p>&nbsp;*&nbsp;<span style="color: black;">@authors</span>&nbsp;BaAGee&nbsp;(asm19950109<span style="color: black;">@hotmail</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.com)</p>&nbsp;*&nbsp;<span style="color: black;">@date</span>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;2017-09-22&nbsp;22:33:43</p>&nbsp;*/
    </span><span style="color: black;">//&nbsp;抽象<span style="color: black;">信息</span>接口</span><span style="color: black;">abstract</span>&nbsp;<span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">Message</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;发送器</span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">protected</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;$sender;</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">__construct</span><span style="color: black;">($sender)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;sender=$sender;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;发送<span style="color: black;">信息</span></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">send</span><span style="color: black;">($to,$msg)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$msg=<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;msg($msg);</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">//&nbsp;<span style="color: black;">经过</span>发送器发送<span style="color: black;">信息</span></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">$this</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">-&gt;sender-&gt;send($to,$msg);</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;普通<span style="color: black;">信息</span>类型</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">NormalMessage</span>&nbsp;<span style="color: black;">extends</span>&nbsp;<span style="color: black;">Message</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">msg</span><span style="color: black;">($msg)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">【普通】</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.$msg;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;警告<span style="color: black;">信息</span>类型</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">WarningMessage</span>&nbsp;<span style="color: black;">extends</span>&nbsp;<span style="color: black;">Message</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">msg</span><span style="color: black;">($msg)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">【警告】</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.$msg;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;危险<span style="color: black;">信息</span>类型</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">DangerMessage</span>&nbsp;<span style="color: black;">extends</span>&nbsp;<span style="color: black;">Message</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">msg</span><span style="color: black;">($msg)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">return</span>&nbsp;<span style="color: black;">【危险】</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.$msg;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;发送站内<span style="color: black;">信息</span>发送器</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">SiteMessageSender</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">send</span><span style="color: black;">($to,$msg)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">给"</span>.$to.<span style="color: black;">"发送站内<span style="color: black;">信息</span>:&nbsp;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.$msg.PHP_EOL;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;发送email<span style="color: black;">信息</span>发送器</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">EmailMessageSender</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">send</span><span style="color: black;">($to,$msg)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">给"</span>.$to.<span style="color: black;">"发送Email<span style="color: black;">信息</span>:&nbsp;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.$msg.PHP_EOL;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;发送短信<span style="color: black;">信息</span>发送器</span><span style="color: black;"><span style="color: black;">class</span>&nbsp;<span style="color: black;">SMSMessageSender</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">public</span>&nbsp;<span style="color: black;"><span style="color: black;">function</span>&nbsp;<span style="color: black;">send</span><span style="color: black;">($to,$msg)</span></span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">{</p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: black;">echo</span>&nbsp;<span style="color: black;">给"</span>.$to.<span style="color: black;">"发送短信<span style="color: black;">信息</span>:&nbsp;</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">.$msg.PHP_EOL;</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;&nbsp;&nbsp;&nbsp;}</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">}</p><span style="color: black;">//&nbsp;总共有9种组合方式,<span style="color: black;">然则</span>仅仅需要6个类</span>$normal_msg=<span style="color: black;">new</span>NormalMessage(<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;SiteMessageSender());</p>$normal_msg-&gt;send(<span style="color: black;">小红</span>,<span style="color: black;">normal信息信息信息</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>$normal_msg=<span style="color: black;">new</span>&nbsp;NormalMessage(<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;EmailMessageSender());</p>$normal_msg-&gt;send(<span style="color: black;">小红</span>,<span style="color: black;">normal信息信息信息</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>$normal_msg=<span style="color: black;">new</span>&nbsp;NormalMessage(<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;SMSMessageSender());</p>$normal_msg-&gt;send(<span style="color: black;">小红</span>,<span style="color: black;">normal信息信息信息</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>$warning_msg=<span style="color: black;">new</span>&nbsp;WarningMessage(<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;SiteMessageSender());</p>$warning_msg-&gt;send(<span style="color: black;">小绿</span>,<span style="color: black;">warning信息信息信息</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>$warning_msg=<span style="color: black;">new</span>&nbsp;WarningMessage(<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;EmailMessageSender());</p>$warning_msg-&gt;send(<span style="color: black;">小绿</span>,<span style="color: black;">warning信息信息信息</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>$warning_msg=<span style="color: black;">new</span>WarningMessage(<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;SMSMessageSender());</p>$warning_msg-&gt;send(<span style="color: black;">小绿</span>,<span style="color: black;">warning信息信息信息</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>$danger_msg=<span style="color: black;">new</span>&nbsp;DangerMessage(<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;SiteMessageSender());</p>$danger_msg-&gt;send(<span style="color: black;">小花</span>,<span style="color: black;">danger信息信息信息</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>$danger_msg=<span style="color: black;">new</span>&nbsp;DangerMessage(<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;EmailMessageSender());</p>$danger_msg-&gt;send(<span style="color: black;">小花</span>,<span style="color: black;">danger信息信息信息</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">);</p>$danger_msg=<span style="color: black;">new</span>&nbsp;DangerMessage(<span style="color: black;">new</span>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">&nbsp;SMSMessageSender());</p>$danger_msg-&gt;send(<span style="color: black;">小花</span>,<span style="color: black;">danger信息信息信息</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;">转载:baagee博客,感谢作者</p>
    <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_jpg/mpTpNCHRiciaicaDQYMQjicuJBTHZVOeRibjicoU4Yyx3tnBY2e7JNoMC3ECDW5zxTuDj0XpXSW42FJ8BNmdNOfamdnA/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>




b1gc8v 发表于 2024-10-6 09:11:45

我们有着相似的经历,你的感受我深有体会。

nqkk58 发表于 2024-10-13 09:28:51

楼主节操掉了,还不快捡起来!

1fy07h 发表于 2024-10-28 10:09:59

“NB”(牛×的缩写,表示叹为观止)‌

nqkk58 发表于 2024-11-9 00:49:43

期待你更多的精彩评论,一起交流学习。

4lqedz 发表于 2024-11-10 08:10:28

我赞同你的看法,你的智慧让人佩服,谢谢分享。
页: [1]
查看完整版本: PHP设计模式-责任链/策略/装饰器/适配器/桥接模式