-
Notifications
You must be signed in to change notification settings - Fork 0
/
Observe.php
48 lines (40 loc) · 1.14 KB
/
Observe.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
<?php
/**
* Created by PhpStorm.
* User: haobin
* Date: 2017/10/9
* Time: 16:26
*/
/**
* 观察者模式:
* 一个对象通过添加一个方法(该方法允许另一个对象,即观察者 注册自己)使本身变得可观察。
* 当可观察的对象更改时,它会将消息发送到已注册的观察者。这些观察者使用该信息执行的操作与可观察的对象无关。
* 结果是对象可以相互对话,而不必了解原因。
*/
interface IObserver {
function onChange($sender, $args);
}
interface IObservable {
function addObserver($observer);
}
class UserList implements IObservable {
private $_obervers = [];
public function addCustomer($name) {
foreach($this->_obervers as $oberver) {
$oberver->onChange($this, $name);
}
}
public function addObserver($observers)
{
$this->_obervers = $observers;
}
}
class UserListLogger implements IObserver {
public function onChange($sender, $args)
{
echo "{$args} added to user list \n";
}
}
$ul = new UserList();
$ul->addObserver(new UserListLogger());
$ul->addCustomer("haobin");