PHP责任链设计模式
<?php //责任链设计模式 /** * 每个对象,储存着对自己上级的引用, * 如果自己处理不了,交给上一级。 */ class board{ protected $power=1; protected $top='admin'; public function process($lev){ if($lev<=$this->power){ echo "版主删帖"; }else{ $top=new $this->top; $top->process($lev); } } } class admin{ protected $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=3; protected $top=null; public function process($lev){ echo '抓起来'; } } // 责任链模式来处理举报问题 $lev=$_POST['jubao']+0; $judge=new board(); $judge->process($lev);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>责任链模式处理举报问题</title> </head> <body> <form action="2.php" method="post"> <select name="jubao"> <option value="1">粗口</option> <option value="2">黄赌毒</option> <option value="3">分裂国家</option> </select> <button type="submit">举报</button> </form> </body> </html>