-
Notifications
You must be signed in to change notification settings - Fork 50
/
mediator.php
65 lines (54 loc) · 1.61 KB
/
mediator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* Created by PhpStorm.
* User: lock
* Date: 2017/8/13
* Time: 22:57
* 中介者模式
* 用一个中介对象来封装一系列的对象交互,使各对象不需要显式地相互引用从而使其耦合松散,而且可以独立地改变它们之间的交互
*/
abstract class Mediator {
abstract public function send($message, $colleague);
}
abstract class Colleague {
private $_mediator = null;
public function __construct($mediator) {
$this->_mediator = $mediator;
}
public function send($message) {
$this->_mediator->send($message, $this);
}
abstract public function notify($message);
}
class Colleague1 extends Colleague {
public function notify($message) {
echo "Colleague1 Message is :" . $message . PHP_EOL;
}
}
class Colleague2 extends Colleague {
public function notify($message) {
echo "Colleague2 Message is :" . $message . PHP_EOL;
}
}
class ConcreteMediator extends Mediator {
private $_colleague1 = null;
private $_colleague2 = null;
public function send($message, $colleague) {
if ($colleague == $this->_colleague1) {
$this->_colleague1->notify($message);
} else {
$this->_colleague2->notify($message);
}
}
public function set($colleague1, $colleague2) {
$this->_colleague1 = $colleague1;
$this->_colleague2 = $colleague2;
}
}
//
$objMediator = new ConcreteMediator();
$objC1 = new Colleague1($objMediator);
$objC2 = new Colleague2($objMediator);
$objMediator->set($objC1, $objC2);
$objC1->send("to c2 from c1");
$objC2->send("to c1 from c2");