往事随疯

生活不止眼前的苟且,还有诗和远方的田野

8、职责链模式

chain of resionbility

    <h1>指责链模式<h1>
        <form method="post" action="">
        <select name="jubao">
            <option value="1">粗口</option>
            <option value="2">黄赌毒</option>
            <option value="3">分裂国家</option>
        </select>
        <input type="submit" value="提交">
    </form>

<?php
    $lev = isset($_POST['jubao']) ? $_POST['jubao'] + 0 : 0 ;

    class board
    {
        public function process()
        {
            echo '版主删帖';
        }
    }
    class admin
    {
        public function process()
        {
            echo '管理员封帐号';
        }
    }
    class police
    {
        public function process()
        {
            echo '抓起来';
        }
    }

    if($lev == 1)
    {
        $judge = new board();
        $judge->process();
    }else if($lev == 2)
    {
        $judge = new admin();
        $judge->process();
    }else if($lev == 3)
    {
        $judge = new police();
        $judge->process();
    }
?>

<h1>指责链模式<h1>
<form method="post" action="">
    <select name="jubao">
        <option value="1">粗口</option>
        <option value="2">黄赌毒</option>
        <option value="3">分裂国家</option>
    </select>
    <input type="submit" value="提交">
</form>
<?php
$lev = isset($_POST['jubao']) ? $_POST['jubao'] + 0 : 0 ;
$judge = new board();
$judge->process($lev);
//责任链模式
class board
{
    public $power = 1;
    protected $top = 'admin';

    public function process($lev)
    {
        if($lev <= $this->power)
        {
            echo '版主删帖';
        }else{
            $top = new $this->top;
            $top->process($lev);
        }
    }
}
class admin
{
    public $power = 2;
    protected $top = 'police';

    public function process($lev)
    {
        if($lev <= $this->power)
        {
            echo '管理员封帐号';
        }else{
            $top = new $this->top;
            $top->process($lev);
        }
    }
}
class police
{
    protected $power;
    protected $top = null;

    public function process()
    {
        echo '抓起来';
    }
}


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29518403/article/details/51549983
文章标签: 设计模式
个人分类: 设计模式
上一篇7、观察者模式
下一篇9、策略模式
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭