-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserver_1.php
159 lines (130 loc) · 3.51 KB
/
observer_1.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
interface Subject{
public function registerObserver(Observer $observer);
public function removeObserver(Observer $observer);
public function notifyObservers();
}
interface Observer{
public function update($temperature, $humidity, $pressure);
}
interface DisplayElement{
public function display();
}
class WeatherData implements Subject{
private $observers;
private $temperature;
private $humidity;
private $pressure;
public function __construct(){
$this->observers=array();
}
public function registerObserver(Observer $observer){
$this->observers[]=$observer;
}
public function removeObserver(Observer $observer){
$index = array_search($observer, $this->observers);
if(false !== $index){
array_splice($this->observers, $index, 1);
}
}
public function notifyObservers(){
foreach ($this->observers as $key => $observer) {
$observer->update($this->temperature, $this->humidity, $this->pressure);
}
}
public function measurementsChange(){
$this->notifyObservers();
}
public function setMeasurements(float $temperature, float $humidity, float $pressure){
$this->temperature=$temperature;
$this->humidity=$humidity;
$this->pressure=$pressure;
$this->measurementsChange();
}
}
class CurrentConditionsDisplay implements Observer{
private $weatherData;
private $temperature;
private $humidity;
private $pressure;
public function __construct(Subject $weatherData){
$this->weatherData=$weatherData;
$this->weatherData->registerObserver($this);
}
public function update($temperature, $humidity, $pressure){
$this->temperature=$temperature;
$this->humidity=$humidity;
$this->pressure=$pressure;
$this->display();
}
public function display(){
echo "Current conditions:".$this->temperature."F degrees and ".$this->humidity."% humidity";
}
}
class ForecastDisplay implements Observer{
private $lastPressure;
private $currentPressure;
private $weatherData;
public function __construct(Subject $weatherData){
$this->weatherData=$weatherData;
$this->weatherData->registerObserver($this);
}
public function update(){
}
}
class WeatherStation{
public function __construct(){
$weatherData = new WeatherData();
new CurrentConditionsDisplay($weatherData);
$weatherData->setMeasurements(80,65, 30.4);
}
}
new WeatherStation();
/*
class NewsPaper implements \SplSubject{
private $name;
private $observers=array();
private $content;
public function __construct($name){
$this->name=$name;
}
public function attach(\SplObserver $observer){
$this->observers[]=$observer;
}
public function detach(\SplObserver $observer){
$index=array_search($observer, $this->observers, true);
//var_dump($this->observers);
//var_dump($index);
if(false !==$index)
unset($this->observers[$index]);
}
public function breakOutNews($content){
$this->content=$content;
$this->notify();
}
public function getContent(){
return $this->content."({$this->name})";
}
public function notify(){
foreach ($this->observers as $key => $observer) {
$observer->update($this);
}
}
}
class Reader implements \SplObserver{
private $name;
public function __construct($name){
$this->name=$name;
}
public function update(\SplSubject $subject){
echo $this->name." is reading breakout news:".$subject->getContent()."\r\n";
}
}
$newsPaper=new NewsPaper('NewYork Times');
$newsPaper->attach(new Reader('Allen'));
$newsPaper->attach(new Reader('Jim'));
$linda=new Reader('Linda');
$newsPaper->attach($linda);
$newsPaper->detach($linda);
$newsPaper->breakOutNews('USA break down!');
*/