首页 > 其他 > 详细

责任链模式

时间:2015-01-18 20:52:35      阅读:277      评论:0      收藏:0      [点我收藏+]

  责任链模式:使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系。将这些对象连成一条链,并沿着这条链传递该请求,直到有一个对象处理它为止。

  示例图:

技术分享

php代码示例:

<?php
class Request{
    public $tcontent,$tnum;
    function __construct($tcontent,$tnum){
        $this -> tcontent = $tcontent;
        $this -> tnum = $tnum;
    }
}

class Manager{
    public $name,$manager;
    function __construct($temp){
        $this -> name = $temp;
    }
    function SetSuccessor($temp){
        $this -> manager = $temp;
    }
    function GetRequest($req){
    }
}

class CommonManager extends Manager{
    function GetRequest($req){
        if($req -> tnum >= 0 && $req -> tnum < 10){
            print $this->name." handled ".$req->tnum." request\n";
        }else{
            $this -> manager -> GetRequest($req);
        }
    }
}

class MajorDomo extends Manager{
    function GetRequest($req){
        if($req -> tnum >= 10){
            print $this->name." handled ".$req->tnum." request\n";
        }
    }
}


$common = new CommonManager("xiao");
$major = new MajorDomo("da");
$common -> SetSuccessor($major);
$req2 = new Request("salary",3);
$common -> GetRequest($req2);
$req1 = new Request("vacation",33);
$common -> GetRequest($req1);
?>

  

责任链模式

原文:http://www.cnblogs.com/zhutianpeng/p/4232163.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!