[php]php设计模式 Observer(观察者模式)
1 <?php
2 /**
3 * 观察者模式
4 *
5 * 定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新
6 * 能够便利地创建查看目标对象状态的对象,并且提供与核心对象非耦合的指定功能
7 * 插件系统
8 */
9 class Observerable
10 {
11 private$_observers=array();
12
13 publicfunction registerObserver($observer)
14 {
15 $this->_observers[] =$observer;
16 }
17
18 publicfunction removeObserver($observer)
19 {
20 $key=array_search($observer,$this->_observers);
21 if(!($key===false))
22 {
23 unset($this->_observers[$key]);
24 }
25 }
26
27 publicfunction notifyObservers()
28 {
29 foreach($this->_observers as$observer)
30 {
31 if($observer instanceof Observer) $observer->update($this);
32 }
33 }
34 }
35
36 interface Observer
37 {
38 publicfunction update($observer);
39 }
40
41 interface DisplayElement
42 {
43 publicfunction display();
44 }
45
46 // -- 实例类定义
47 class NewsObserverable extends Observerable
48 {
49 private$_sports_news;
50 publicfunction setSportsNews($data)
51 {
52 $this->_sports_news =$data;
53 $this->notifyObservers();
54 }
55
56 publicfunction getSportsNews()
57 {
58 return$this->_sports_news;
59 }
60
61 private$_local_news;
62 publicfunction setLocalNews($data)
63 {
64 $this->_local_news =$data;
65 $this->notifyObservers();
66 }
67
68 publicfunction getLocalNews()
69 {
70 return$this->_local_news;
71 }
72 }
73
74 class SportsNews implements Observer,DisplayElement
75 {
76 private$_data=null;
77 publicfunction update($observer)
78 {
79 if($this->_data !=$observer->getSportsNews())
80 {
81 $this->_data =$observer->getSportsNews();
82 $this->display();
83 }
84 }
85
86 publicfunction display()
87 {
88 echo$this->_data.date("Y-m-d H:i:s")."<br/>";
89 }
90 }
91
92 class LocalNews implements Observer,DisplayElement
93 {
94 private$_data=null;
95 publicfunction update($observer)
96 {
97 if($this->_data !=$observer->getLocalNews())
98 {
99 $this->_data =$observer->getLocalNews();
100 $this->display();
101 }
102 }
103
104 publicfunction display()
105 {
106 echo$this->_data.date("Y-m-d H:i:s")."<br/>";
107 }
108 }
109
110 // -- 实例化 ---
111
112 $objObserver=new NewsObserverable();
113 $local=new LocalNews();
114 $sports=new SportsNews();
115
116 $objObserver->registerObserver($local);
117 $objObserver->registerObserver($sports);
118
119 $objObserver->setSportsNews("sports news 1 ");
120 $objObserver->setLocalNews("local news 1 ");
121 $objObserver->removeObserver($sports);
122 $objObserver->setLocalNews("local news 2 ");
123 $objObserver->setSportsNews("sports news 2 ");
124 $objObserver->removeObserver($local);
125 $objObserver->setLocalNews("local news 3 ");
2 /**
3 * 观察者模式
4 *
5 * 定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新
6 * 能够便利地创建查看目标对象状态的对象,并且提供与核心对象非耦合的指定功能
7 * 插件系统
8 */
9 class Observerable
10 {
11 private$_observers=array();
12
13 publicfunction registerObserver($observer)
14 {
15 $this->_observers[] =$observer;
16 }
17
18 publicfunction removeObserver($observer)
19 {
20 $key=array_search($observer,$this->_observers);
21 if(!($key===false))
22 {
23 unset($this->_observers[$key]);
24 }
25 }
26
27 publicfunction notifyObservers()
28 {
29 foreach($this->_observers as$observer)
30 {
31 if($observer instanceof Observer) $observer->update($this);
32 }
33 }
34 }
35
36 interface Observer
37 {
38 publicfunction update($observer);
39 }
40
41 interface DisplayElement
42 {
43 publicfunction display();
44 }
45
46 // -- 实例类定义
47 class NewsObserverable extends Observerable
48 {
49 private$_sports_news;
50 publicfunction setSportsNews($data)
51 {
52 $this->_sports_news =$data;
53 $this->notifyObservers();
54 }
55
56 publicfunction getSportsNews()
57 {
58 return$this->_sports_news;
59 }
60
61 private$_local_news;
62 publicfunction setLocalNews($data)
63 {
64 $this->_local_news =$data;
65 $this->notifyObservers();
66 }
67
68 publicfunction getLocalNews()
69 {
70 return$this->_local_news;
71 }
72 }
73
74 class SportsNews implements Observer,DisplayElement
75 {
76 private$_data=null;
77 publicfunction update($observer)
78 {
79 if($this->_data !=$observer->getSportsNews())
80 {
81 $this->_data =$observer->getSportsNews();
82 $this->display();
83 }
84 }
85
86 publicfunction display()
87 {
88 echo$this->_data.date("Y-m-d H:i:s")."<br/>";
89 }
90 }
91
92 class LocalNews implements Observer,DisplayElement
93 {
94 private$_data=null;
95 publicfunction update($observer)
96 {
97 if($this->_data !=$observer->getLocalNews())
98 {
99 $this->_data =$observer->getLocalNews();
100 $this->display();
101 }
102 }
103
104 publicfunction display()
105 {
106 echo$this->_data.date("Y-m-d H:i:s")."<br/>";
107 }
108 }
109
110 // -- 实例化 ---
111
112 $objObserver=new NewsObserverable();
113 $local=new LocalNews();
114 $sports=new SportsNews();
115
116 $objObserver->registerObserver($local);
117 $objObserver->registerObserver($sports);
118
119 $objObserver->setSportsNews("sports news 1 ");
120 $objObserver->setLocalNews("local news 1 ");
121 $objObserver->removeObserver($sports);
122 $objObserver->setLocalNews("local news 2 ");
123 $objObserver->setSportsNews("sports news 2 ");
124 $objObserver->removeObserver($local);
125 $objObserver->setLocalNews("local news 3 ");
think in coding