Created
October 8, 2017 16:11
-
-
Save tkouleris/a4512eb718d2e5eb557c66bacff2eff8 to your computer and use it in GitHub Desktop.
Observe Pattern Implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Observer Design Pattern - Weather Station Example | |
| */ | |
| abstract class Observer{ | |
| public function update(){} | |
| } | |
| abstract class Subject{ | |
| public function register(Observer $obsInstance ){} | |
| public function notify(){} | |
| } | |
| class SmartPhone extends Observer{ | |
| private $displayTemperature; | |
| private $displayHumidity; | |
| private $displayPressure; | |
| private $SubjectInstance; | |
| public function __construct(Subject $subjInstance){ | |
| $this->SubjectInstance = $subjInstance; | |
| // Init Values | |
| $this->displayTemperature = 0; | |
| $this->displayHumidity = 0; | |
| $this->displayPressure = 0; | |
| } | |
| public function update(){ | |
| $this->displayTemperature = $this->SubjectInstance->getTemperature(); | |
| $this->displayHumidity = $this->SubjectInstance->getHumidity(); | |
| $this->displayPressure = $this->SubjectInstance->getPressure(); | |
| echo "\r\n========= SmartPhone Display ============="; | |
| $this->display(); | |
| echo "\r\n===========================================\r\n"; | |
| } | |
| public function display(){ | |
| echo "\r\nSmartPhone Temperature: ".$this->displayTemperature; | |
| echo "\r\nSmartPhone Humidity: ".$this->displayHumidity; | |
| echo "\r\nSmartPhone Pressure: ".$this->displayPressure; | |
| } | |
| } | |
| class SmartWatch extends Observer{ | |
| private $displayTemperature; | |
| private $displayHumidity; | |
| private $displayPressure; | |
| private $SubjectInstance; | |
| public function __construct(Subject $subjInstance){ | |
| $this->SubjectInstance = $subjInstance; | |
| // Init Values | |
| $this->displayTemperature = 0; | |
| $this->displayHumidity = 0; | |
| $this->displayPressure = 0; | |
| } | |
| public function update(){ | |
| $this->displayTemperature = $this->SubjectInstance->getTemperature(); | |
| $this->displayHumidity = $this->SubjectInstance->getHumidity(); | |
| $this->displayPressure = $this->SubjectInstance->getPressure(); | |
| echo "\r\n========= SmartWatch Display ============="; | |
| $this->display(); | |
| echo "\r\n===========================================\r\n"; | |
| } | |
| public function display(){ | |
| echo "\r\nSmartWatch Temperature: ".$this->displayTemperature; | |
| echo "\r\nSmartWatch Humidity: ".$this->displayHumidity; | |
| echo "\r\nSmartWatch Pressure: ".$this->displayPressure; | |
| } | |
| } | |
| class WeatherStation extends Subject{ | |
| private $ObserverArray; | |
| private $Temperature; | |
| private $Humidity; | |
| private $Pressure; | |
| public function __construct(){ | |
| $this->ObserverArray = array(); | |
| // Init Values | |
| $this->Temperature = 0; | |
| $this->Humidity = 0; | |
| $this->Pressure = 0; | |
| } | |
| public function register(Observer $obsInstance){ | |
| array_push($this->ObserverArray, $obsInstance); | |
| } | |
| public function getTemperature(){ | |
| return $this->Temperature; | |
| } | |
| public function getHumidity(){ | |
| return $this->Humidity; | |
| } | |
| public function getPressure(){ | |
| return $this->Pressure; | |
| } | |
| public function setValues($temp, $hum, $press){ | |
| $this->Temperature = $temp; | |
| $this->Humidity = $hum; | |
| $this->Pressure = $press; | |
| $this->notify(); | |
| } | |
| public function notify(){ | |
| foreach ($this->ObserverArray as $ObserverObject) { | |
| $ObserverObject->update(); | |
| } | |
| } | |
| } | |
| //Client Code | |
| $WeatherStation = new WeatherStation(); | |
| $SmartWatch = new SmartWatch($WeatherStation); | |
| $SmartPhone = new SmartPhone($WeatherStation); | |
| $WeatherStation->register($SmartPhone); | |
| $WeatherStation->register($SmartWatch); | |
| $WeatherStation->setValues(18,102.2, 52); | |
| $WeatherStation->setValues(20,103.1, 70); | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment