php设计模式之责任链模式实现举报功能实例代码

html

<html>
<head>
    <meta charset="UTF-8">
    <title>责任链模式</title>
</head>
<body>

<h1>责任链模式举报</h1>
    <form action="09-2.php" method="post">
        <select name="lev" id="">
            <option value="1">粗口</option>
            <option value="2">黄赌毒</option>
            <option value="3">国家分裂</option>
        </select>
        <button type="submit">举报</button>
    </form>
</body>
</html>

php

<?php 
header("Content-type:text/html;charset=utf-8");

/**
* 版主
*/
class Broad
{
    protected $power = 1;
    protected $prev = 'Admin';
    public function process($lev)
    {
        if ($lev <= $this->power) {
            echo "版主删帖";
        }else{
            $process = new $this->prev;
            $process->process($lev);
        }
        
    }
}

/**
* 管理员
*/
class Admin
{
    protected $power = 2;
    protected $prev = 'Police';
    public function process($lev)
    {
        if ($lev <= $this->power) {
            echo "封号处理";
        }else{
            $process = new $this->prev;
            $process->process($lev);
        }
    }
}

/**
* 公安
*/
class Police
{
    protected $power;
    protected $prev ;
    public function process($lev)
    {
        echo "被公安抓了哈";
    }
}

$lev = $_POST['lev'];
$process = new Broad();
$process->process($lev);
posted @ 2020-01-12 12:17  A毛毛  阅读(176)  评论(0编辑  收藏  举报