php中的设计模式之--中介模式 mediator = $med; $this->name = $name; } public abstract function send($msg); public abstract function receive($msg);} class qqchat implements chatmediator { // 用户列表 private $users; public function __construct(){ $this->users = null ; } // 添加用户 public function addqquser($user){ $this->users[] = $user; } // 发送具体的qq信息通过qq发送 public function sendmessage($msg, $user) { foreach($this->users as $k =>$v){ // 自己发送的自己不能接受 if($v != $user){ // 调用好友的接口 $v->receive($msg); } } } }// 具体对象角色 class qquser extends user { public function send($msg){ $this->mediator->sendmessage($msg, $this); } // 接受 public function receive($msg) { echo $this->name.' receive '.$msg.'
' ; } } // client // 中介为qq $qqchat = new qqchat(); $ome = new qquser($qqchat, 张三); $ofriend1 = new qquser($qqchat, 李四); $ofriend2 = new qquser($qqchat, 王伟); $ofriend3 = new qquser($qqchat, 大哥); // 添加好友 $qqchat->addqquser($ome); $qqchat->addqquser($ofriend1); $qqchat->addqquser($ofriend2); $qqchat->addqquser($ofriend3); $ome->send(hi all);
http://www.bkjia.com/phpjc/953321.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/953321.htmltecharticlephp中的设计模式之--中介模式 朋友1 我 qq 朋友2 朋友13*/// 抽象中介,qq聊天 interface chatmediator { // 中介者角色 public function sendmessage($msg,$user)...