search
Homephp教程php手册php中的设计模式之--中介模式

<?php 

/*
中介者模式是一种行为型模式,它包装了一系列对象相互作用的方式,
使得这些对象不必相互明显作用,从而使它们可以松散偶合。当某些对象之间的作用发生改变时,
不会立即影响其他的一些对象之间的作用,保证这些作用可以彼此独立的变化。
定义一个中介对象来封装系列对象之间的交互。中介者使各个对象不需要显示地相互引用,从而使其耦合性松散,而且可以独立地改变他们之间的交互。

主要角色
?	中介者(Mediator)角色:定义了对象间相互作用的接口(房产中介)
?	具体中介者(ConcreteMediator)角色:实现了中介者定义的接口。
.   如果一个对象会影响其他的对象,同时也会被其他对象影响,那么这两个对象称为同事类
?	具体对象(ConcreteColleague)角色:通过中介者和别的对象进行交互(房东)

中介者模式的优点
	适当地使用中介者模式可以避免同事类之间的过度耦合,使得各同事类之间可以相对独立地使用。
	使用中介者模式可以将对象间一对多的关联转变为一对一的关联,使对象间的关系易于理解和维护。
	使用中介者模式可以将对象的行为和协作进行抽象,能够比较灵活的处理对象间的相互作用。
	用一个中介对象来封装一系列的对象交互,使各对象不需要显式地相互引用从而使其耦合松散,而且可以
	独立地改变它们之间的交互
	 

QQ聊天系统就是一个中介模式 ,
		<---->朋友1 
我<-->QQ <---->朋友2 
	   	<---->朋友13

*/

// 抽象中介,QQ聊天 

 interface ChatMediator { // 中介者角色
      public function  sendMessage($msg,$user);
      public function  addQQUser($user);
} 


// 抽象用户
 abstract class User {
    protected  $mediator;
    protected  $name;
     
    public function __construct($med, $name){
        $this->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.&#39;  receive &#39;.$msg.&#39;<br/>&#39; ;
    }
 
}
 

 
 // 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");
		
		









Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.