本代码根据《Head First设计模式》一书中的java代码改写而来。
策略模式
观察者模式
<?php
/**
* Created by PhpStorm.
* User: shindou
* Date: 16/7/14
* Time: 下午10:19
*/
namespace Observer;
interface Subject{
public function registerObserver($o);//Observer
public function removeObserver($o);//Observer
public function notifyObserver();
}
//
interface Observer{
public function update($temperature,$humidity,$pressure);//float temp
}
interface DisplayElement{
public function display();
}
//
class WeatherData implements Subject{
private $observers;
private $temperature;
private $humidity;
private $pressure;
public function registerObserver($o){
// TODO: Implement registerObserver() method.
$this->observers[] = $o;
}
//
public function removeObserver($o){
// TODO: Implement removeObserver() method.
unset($this->observers[$o]);
}
//
public function notifyObserver(){
// TODO: Implement notifyObserver() method.
foreach($this->observers as $observer){
$observer->update($this->temperature,$this->humidity,$this->pressure);
}
}
public function measurementsChanged(){
$this->notifyObserver();
}
public function setMeasurements($temperature,$humidity,$pressure){
$this->temperature = $temperature;
$this->humidity = $humidity;
$this->pressure = $pressure;
$this->measurementsChanged();
}
}
class CurrentConditionsDisplay implements Observer,DisplayElement{
private $temperature;//float
private $humidity;//float
private $weatherData;//subject
public function __construct(Subject $weatherData){
$this->weatherData = $weatherData;
$this->weatherData->registerObserver($this);
}
public function update($tempperature,$humidity,$pressure){
// TODO: Implement update() method.
$this->temperature = $tempperature;
$this->humidity = $humidity;
$this->display();
}
public function display(){
// TODO: Implement display() method.
echo("Current conditions:".$this->temperature."F degrees and ".$this->humidity."% humidity\n");
}
}
$WeatherData = new WeatherData();
$currentDisplay = new CurrentConditionsDisplay($WeatherData);
$WeatherData->setMeasurements(80,65,'30.4f');
$WeatherData->setMeasurements(82,70,'29.2f');
$WeatherData->setMeasurements(78,90,'29.2f');
装饰者模式
<?php
/**
* Created by PhpStorm.
* User: shindou
* Date: 16/7/16
* Time: 下午10:19
*/
namespace Decorate;
abstract class Beverage {
protected $description = "Unknown Beverage";
/**
* @return string
*/
public function getDescription(){
return $this->description;
}
public abstract function cost();
}
abstract class CondimentDecorator extends Beverage{
//php中抽象类无法重写原抽象类中的
}
class Espresso extends Beverage{
public function __construct(){
$this->description = "Espresso";
}
public function cost(){
return 1.99;
}
}
class DarkRoast extends Beverage{
public function __construct(){
$this->description = "DarkRoast Coffee";
}
public function cost(){
return .99;
}
}
class HouseBlend extends Beverage{
public function __construct(){
$this->description = "House Blend Coffee";
}
public function cost(){
return .89;
}
}
class Mocha extends CondimentDecorator{
protected $beverage;//Beverage类型
public function __construct(Beverage $beverage){
$this->beverage = $beverage;
}
public function getDescription(){
// TODO: Implement getDescription() method.
return $this->beverage->getDescription().", Mocha";
}
public function cost(){
// TODO: Implement cost() method.
return .20 + $this->beverage->cost();
}
}
class Soy extends CondimentDecorator{
protected $beverage;
public function __construct(Beverage $beverage){
$this->beverage = $beverage;
}
public function getDescription(){
return $this->beverage->getDescription().", Soy";
}
public function cost(){
// TODO: Implement cost() method.
return .15 + $this->beverage->cost();
}
}
class Whip extends CondimentDecorator{
protected $beverage;
public function __construct(Beverage $beverage){
$this->beverage = $beverage;
}
public function getDescription(){
return $this->beverage->getDescription().", Whip";
}
public function cost(){
// TODO: Implement cost() method.
return .10 + $this->beverage->cost();
}
}
$beverage = new Espresso();
echo ($beverage->getDescription()." $".$beverage->cost()."\n");
$beverage2 = new DarkRoast();
$beverage2 = new Mocha($beverage2);
$beverage2 = new Mocha($beverage2);
$beverage2 = new Whip($beverage2);
echo ($beverage2->getDescription()." $".$beverage2->cost()."\n");
$beverage3 = new HouseBlend();
$beverage3 = new Soy($beverage3);
$beverage3 = new Mocha($beverage3);
$beverage3 = new Whip($beverage3);
echo ($beverage3->getDescription()." $".$beverage3->cost()."\n");