Head First设计模式PHP版

本代码根据《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");

工厂模式

单件模式

命令模式

适配器与外观模式

迭代器与组合模式

状态模式

代理模式

复合模式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值