public static function main(array $args = array()) { $weatherData = new WeatherData(); $currentDisplay = new CurrentConditionsDisplay($weatherData); $statisticsDisplay = new StatisticsDisplay($weatherData); $forecastDisplay = new ForecastDisplay($weatherData); $weatherData->setMeasurements(80, 65, 30.4); $weatherData->setMeasurements(82, 70, 29.2); $weatherData->setMeasurements(78, 90, 29.2); }
public static function index() { $weatherData = new WeatherData(); $currentDisplay = new CurrentConditionsDisplay($weatherData); $statisticsDisplay = new StatisticsDisplay($weatherData); $forecastDisplay = new ForecastDisplay($weatherData); $heatIndexDisplay = new HeatIndexDisplay($weatherData); $weatherData->setMeasurements(80, 65, 30.4); $weatherData->setMeasurements(82, 70, 29.2); $weatherData->setMeasurements(78, 90, 29.2); }
<?php ini_set('display_errors', "On"); error_reporting(E_ALL | E_STRICT); //自动加载类 set_include_path(get_include_path() . PATH_SEPARATOR . 'class/'); //设置加载路径 spl_autoload_extensions('.php'); //设置加载后缀名 function myAutoload($className) { require_once $className . '.php'; //直接根据类名跟文件关系包含文件 } spl_autoload_register("myAutoload"); //注册自动加载函数 //测试代码开始 $weatherData = new WeatherData(); //首先建立一个天气数据对象 $displayCurrent = new DisplayCurrentConditions($weatherData); $weatherData->setMeasurements(80, 65, '30.4f'); $weatherData->setMeasurements(82, 70, '29.2f'); $weatherData->setMeasurements(78, 90, '29.2f'); echo '<hr>'; echo file_get_contents('./read.txt');
public function update($temperature, $humidity, $pressure) { $this->temperature = $temperature; $this->humidity = $humidity; $this->display(); } public function display() { println('Current Contiditions:' . $this->temperature . ' F degrees and ' . $this->humidity . '% humidity'); } } println(SEPARATE, '观察者模式', SEPARATE); showImgs('2.png', '2_1.png', '2_2.png'); $weatherObj = new WeatherData(); $currentConditions = new CurrentConditionsDisplay($weatherObj); $weatherObj->setMeasurements(80, 65, '30.4f'); $weatherObj->setMeasurements(82, 70, '29.2f'); /** * 输出: Current Contiditions:80 F degrees and 65% humidity Current Contiditions:82 F degrees and 70% humidity */ #方法二:可观察者和观察者:观察者拉状态 #可观察者超类 interface Observerable { public function registerObserver($observer); #注册观察者 public function removeObserver($observer); #注销观察者 public function notifyObserver();
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); }
<?php function __autoload($class_name) { include $class_name . '.php'; } $weatherData = new WeatherData(); $currentDisplay = new CurrentConditionDisplay($weatherData); $statisicsDisplay = new StatisticsDisplay($weatherData); $forecastDisplay = new ForecastDisplay($weatherData); $weatherData->setMeasurements(80, 65, 30); $weatherData->setMeasurements(82, 70, 29); $weatherData->setMeasurements(78, 90, 28);
$this->pressure = $press; $this->measurementsChanged(); } } class CurrentConditionsDisplay implements iObserver, iDisplayElement { private $temperature; private $humidity; private $weatherData; function __construct($wd) { $this->weatherData = $wd; $wd->registerObserver($this); } public function update($temp, $humid, $press) { $this->temperature = $temp; $this->humidity = $humid; $this->display(); } public function display() { print "Current conditions: " . $this->temperature . "F degrees and " . $this->humidity . "% humidity\n"; } } $weatherData = new WeatherData(); $currentDisplay1 = new CurrentConditionsDisplay($weatherData); $currentDisplay2 = new CurrentConditionsDisplay($weatherData); $weatherData->setMeasurements(80, 65, 30.4); $weatherData->setMeasurements(82, 70, 29.2); $weatherData->setMeasurements(78, 90, 29.2);