关闭

8、职责链模式

标签: 设计模式
96人阅读 评论(0) 收藏 举报
分类:
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 '抓起来';
    }
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15675次
    • 积分:527
    • 等级:
    • 排名:千里之外
    • 原创:38篇
    • 转载:1篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论