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); }
public function onGet() { $day = date("j", strtotime("now")); $weatherData = array(); $weatherDataModel = WeatherData::where("day", $day)->first(); if ($weatherDataModel) { $weatherData = $weatherDataModel->toArray(); } return View::make('home.index', array("day" => $day, "maxTemp" => array_get($weatherData, "maxTemp", "Unavailable"), "minTemp" => array_get($weatherData, "minTemp", "Unavailable"), "avgTemp" => array_get($weatherData, "avgTemp", "Unavailable"))); }
public function onConfirm() { $weatherDataHeaderList = Session::get("uploadedLeatherDataHeaderList"); $weatherDataMapping = Session::get("uploadedWeatherData"); $cleanedWeatherDataHeaderList = array_map(function ($weatherDataHeader) { $weatherDataHeader = strtolower($weatherDataHeader); return array_get(self::$WEATHERDATAHEADER_DBCOLUMNS, $weatherDataHeader, $weatherDataHeader); }, $weatherDataHeaderList); $databaseWeatherDataMapping = array_map(function ($weatherData) use($cleanedWeatherDataHeaderList) { return array_combine($cleanedWeatherDataHeaderList, $weatherData); }, $weatherDataMapping); WeatherData::truncate(); WeatherData::insert($databaseWeatherDataMapping); return Redirect::to("/"); }
function __construct(WeatherData $weatherData) { $this->weatherData = $weatherData; $weatherData->registerObserver($this); }
<?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 onGet() { $day = date("j", strtotime("now")); $weatherDataList = WeatherData::all()->toArray(); return View::make('report.index', array("weatherDataList" => $weatherDataList, "day" => $day)); }
public function getObserver() { echo "<pre>"; var_dump($this->observer); echo "</pre>"; } public function notifyObserver() { } } interface Observer { public function update(); } class CurrentConditionsDisplay implements Observer { private $weatherObj; public function __construct($weatherData) { $this->weatherObj = $weatherData; $this->weatherObj->registerObserver($this); } public function update() { } } $weatherObj = new WeatherData(); $currentConditions = new CurrentConditionsDisplay($weatherObj); $weatherObj->getObserver(); $weatherObj->removeObserver($currentConditions); $weatherObj->getObserver();
$this->weatherObj->registerObserver($this); } 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 __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);