/
observer_pattern.php
150 lines (129 loc) · 4 KB
/
observer_pattern.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
143
144
145
146
147
148
149
150
<?php
/**
* Interface Subject
*
* A subject can register, remove and notify observers
*/
interface Subject{
public function registerObserver(Observer $o);
public function removeObserver(Observer $o);
public function notifyObservers();
}
/**
* Interface Observer
*
* Observers get updated by the subject when there is a change
*/
interface Observer{
public function update($temp, $humidity, $pressure);
}
/**
* Interface DisplayElement
*
* Display Elements can display data
*/
interface DisplayElement{
public function display();
}
/**
* Class WeatherData
*
* The weather data subject will keep the current temperature humidity and pressure
*/
class WeatherData implements Subject{
private $observers;
private $temperature;
private $humidity;
private $pressure;
public function __construct(){
$this->observers = new SplObjectStorage();
}
public function registerObserver(Observer $o){
$this->observers->attach($o);
}
public function removeObserver(Observer $o){
$this->observers->detach($o);
}
public function notifyObservers(){
foreach($this->observers as $ob){
$ob->update($this->temperature, $this->humidity, $this->pressure);
}
}
public function measurementsChanged(){
$this->notifyObservers();
}
public function setMeasurements($temperature, $humidity, $pressure){
$this->temperature = $temperature;
$this->humidity = $humidity;
$this->pressure = $pressure;
$this->measurementsChanged();
}
}
/**
* Class CurrentConditionsDisplay
*
* The current conditions display is notified by the weather data when there is a
* change in temperature humidity or pressure.
*
* It will display the current weather conditions
*/
class CurrentConditionsDisplay implements Observer, DisplayElement{
private $temperature;
private $humidity;
private $weatherData;
public function __construct(Subject $weatherData){
$this->weatherData = $weatherData;
$weatherData->registerObserver($this);
}
public function update($temperature, $humidity, $pressure){
$this->temperature = $temperature;
$this->humidity = $humidity;
$this->display();
}
public function display(){
print "Current conditions: ".$this->temperature."F degrees and ".$this->humidity."% humidity".PHP_EOL;
}
}
/**
* Class StatisticsDisplay
*
* A statistics display is notified by the weather data when there is a
* change in temperature humidity or pressure.
*
* It will display the average for all three data points.
*/
class StatisticsDisplay implements Observer, DisplayElement{
private $temperature = array();
private $humidity = array();
private $pressure = array();
private $weatherData;
public function __construct(Subject $weatherData){
$this->weatherData = $weatherData;
$weatherData->registerObserver($this);
}
public function update($temperature, $humidity, $pressure){
$this->temperature[] = $temperature;
$this->humidity[] = $humidity;
$this->pressure[] = $pressure;
$this->display();
}
public function display(){
$avg_temperature = array_sum($this->temperature) / count($this->temperature);
$avg_humidity = array_sum($this->humidity) / count($this->humidity);
$avg_pressure = array_sum($this->pressure) / count($this->pressure);
print "Average temperature {$avg_temperature}F degrees".PHP_EOL;
print "Average humidity {$avg_humidity}% humidity".PHP_EOL;
print "Average pressure {$avg_pressure}".PHP_EOL;
}
}
class WeatherStation{
public function __construct(){
$weatherData = new WeatherData();
new CurrentConditionsDisplay($weatherData);
new StatisticsDisplay($weatherData);
$weatherData->setMeasurements(80, 65, 30.4);
$weatherData->setMeasurements(82, 70, 29.2);
$weatherData->setMeasurements(78, 90, 29.2);
}
}
new WeatherStation();