Home > Backend Development > PHP Tutorial > A brief discussion on the responsibility chain model of PHP design pattern

A brief discussion on the responsibility chain model of PHP design pattern

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 08:58:46
Original
975 people have browsed it

Chain of Responsibility Model

Principle: Frankly speaking, it means handling things according to different levels. If you can't handle it yourself, leave it to your superiors. This approach is called the chain of responsibility model.

Application scenario: Asking for leave in the OA management system. Forum report.

<code><form action=<span>'make.php'</span><span>method</span>=<span>'post'</span>>
    <select name=<span>'lev'</span>>
        <<span>option</span><span>value</span>=<span>'1'</span>>脏话</<span>option</span>>
        <<span>option</span><span>value</span>=<span>'2'</span>>黄赌毒</<span>option</span>>
        <<span>option</span><span>value</span>=<span>'3'</span>>反动国家</<span>option</span>>
    </select>
    <inout <span><span>type</span>=</span><span>'submit'</span><span>value</span>=<span>'举报'</span>>
</form></code>
Copy after login

make.php

<code><span><span><?php</span><span>//版主权限</span><span><span>class</span><span>admin</span>{</span><span>protected</span><span>$power</span>=<span>'1'</span>;
        <span>protected</span><span>$top</span> = <span>'submits'</span>; 
        <span>public</span><span><span>function</span><span>make</span><span>(<span>$lev</span>)</span>{</span><span>if</span>(<span>$lev</span><=<span>$this</span>->power){
                <span>echo</span><span>'删帖子'</span>;
            }<span>else</span>{
                <span>$topObj</span> = <span>new</span><span>$this</span>->top;
                <span>$topObj</span>->make(<span>$lev</span>);
            }
        }
    }
    <span>//管理员权限</span><span><span>class</span><span>submits</span>{</span><span>protected</span><span>$power</span>=<span>'3'</span>;
        <span>protected</span><span>$top</span> = <span>'police'</span>; 
        <span>public</span><span><span>function</span><span>make</span><span>(<span>$lev</span>)</span>{</span><span>if</span>(<span>$lev</span><=<span>$this</span>->power){
                <span>echo</span><span>'禁止用户发言'</span>;
            }<span>else</span>{
                <span>$topObj</span> = <span>new</span><span>$this</span>->top;
                <span>$topObj</span>->make(<span>$lev</span>);
            }
        }
    }
    <span>//警察权限</span><span>//责任链最高层(可以不用判断,责任链模式必须要用到最高一层)</span><span><span>function</span><span>police</span>{</span><span><span>function</span><span>make</span><span>(<span>$lev</span>)</span>{</span><span>echo</span><span>'教育处理'</span>;
        }
    }
    <span>//一般是判断权限再去调用区中的类去处理</span><span>//这里的责任链模式</span><span>$lev</span> = <span>$_POST</span>[<span>'lev'</span>];
    <span>$adminObj</span> = <span>new</span> admin();
    <span>$adminObj</span>->make(<span>$lev</span>);
<span>?></span></span></span></code>
Copy after login

Advantages:

  1. There is no need to make judgments in php, just call the bottom layer directly.
  2. Can simplify interconnections in objects.
  3. New request classes can be added at any time.

Disadvantages:

  1. System optimization will be affected
  2. Code debugging is inconvenient
  3. May cause recycling

Summary:

In the chain of responsibility model, many objects are controlled by each object References are connected to form a chain. The request is passed up the chain until an object in the chain decides to handle the request. The client making the request does not know which object in the chain ultimately handles the request, which allows the system to dynamically reorganize the chain and assign responsibilities without affecting the client.

The main advantage of the responsibility chain model is that it can reduce the coupling of the system, simplify the interconnection of objects, and at the same time enhance the flexibility of assigning responsibilities to objects. It is also convenient to add new request processing classes; its main disadvantage is that it cannot guarantee that the request will be certain. is received, and for a relatively long chain of responsibilities, the processing of the request may involve multiple processing objects, system performance will be affected to a certain extent, and it is inconvenient during code debugging.

Applicable situations of the chain of responsibility model include: multiple objects can handle the same request, and which object handles the request is automatically determined at runtime; submitting a request to one of multiple objects without explicitly specifying the recipient Request; a set of objects can be dynamically specified to handle the request.

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above has introduced a brief discussion of the chain of responsibility model of PHP design patterns, including various aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template