《Head First 设计模式》代码之PHP版(面向对象学习)

 更新时间:2010年06月27日 01:34:54   作者:  
《Head First 设计模式》是本不错的讲解设计模式的书,不像F4写的那么枯燥,应该算是比较容易理解的好书。


观察者模式
复制代码 代码如下:

<?php
/**
* 观察者模式
* 定义了对象之间的一对多依赖,当一个对象改变状态时,
* 它的所有依赖者都会收到通知并自动更新。
*/
interface Subject {
public function registerObserver(Observer $o);
public function removeObserver(Observer $o);
public function notifyObservers();
}
interface Observer {
public function update($temperature, $humidity, $pressure);
}
interface DisplayElement {
public function display();
}
class WeatherData implements Subject {
private $observers = array();
private $temperature;
private $humidity;
private $pressure;
public function __construct() {
$this->observers = array();
}
public function registerObserver(Observer $o) {
$this->observers[] = $o;
}
public function removeObserver(Observer $o) {
if (($key = array_search($o, $this->observers)) !== false) {
unset($this->observers[$key]);
}
}
public function notifyObservers() {
foreach ($this->observers as $observer) {
$observer->update($this->temperature, $this->humidity, $this->pressure);
}
}
public function measurementsChanged() {
$this->notifyObservers();
}
public function setMeasurements($temperature, $humidity, $pressure) {
$this->temperature = $temperature;
$this->humidity = $humidity;
$this->pressure = $pressure;
$this->measurementsChanged();
}
}
class CurrentConditionsDisplay implements Observer, DisplayElement {
private $temperature;
private $humidity;
private $weatherData;
public function __construct(Subject $weatherData) {
$this->weatherData = $weatherData;
$weatherData->registerObserver($this);
}
public function update($temperature, $humidity, $pressure) {
$this->temperature = $temperature;
$this->humidity = $humidity;
$this->display();
}
public function display() {
echo "温度:" . $this->temperature . "; 湿度:" . $this->humidity . "%\n";
}
}
class StatistionsDisplay implements Observer, DisplayElement {
private $temperature;
private $humidity;
private $pressure;
private $weatherData;
public function __construct(Subject $weatherData) {
$this->weatherData = $weatherData;
$weatherData->registerObserver($this);
}
public function update($temperature, $humidity, $pressure) {
$this->temperature = $temperature;
$this->humidity = $humidity;
$this->pressure = $pressure;
$this->display();
}
public function display() {
echo "温度:" . $this->temperature . "; 湿度:" . $this->humidity . "%; 气压:" . $this->pressure . "\n";
}
}
$weatherData = new WeatherData();
$currentDisplay = new CurrentConditionsDisplay($weatherData);
$statistionDisplay = new StatistionsDisplay($weatherData);
$weatherData->setMeasurements(10, 20, 30);
$weatherData->removeObserver($currentDisplay);
$weatherData->setMeasurements(30, 40, 50);
?>

命令模式
复制代码 代码如下:

<?php

class Light {
public function __construct() {

}

public function on() {
echo "Light On\n";
}

public function off() {
echo "Light Off\n";
}
}

interface Command {
public function execute();
}

class LightOnCommand implements Command {
public $light;

public function __construct(Light $light) {
$this->light = $light;
}

public function execute() {
$this->light->on();
}
}

class SimpleRemoteControl {
public $slot;

public function __construct() {

}

public function setCommand(Command $command) {
$this->slot = $command;
}

public function buttonWasPressed() {
$this->slot->execute();
}
}

class RemoteControlTest {
public static function main() {
$remote = new SimpleRemoteControl();
$light = new Light();
$lightOn = new LightOnCommand($light);
$remote->setCommand($lightOn);
$remote->buttonWasPressed();
}
}

RemoteControlTest::main();

?>

装饰者模式
复制代码 代码如下:

<?php
/**
* 装饰着模式
* 动态地将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
*/
abstract class Beverage {
public $description = "Unknown Beverage";

public function getDescription() {
return $this->description;
}

public abstract function cost();
}

abstract class CondimentDecorator extends Beverage {
//JAVA代码里这里是个抽象类,PHP不允许这么做
public function getDescription() {
return $this->description;
}
}

class Espresso extends Beverage {
public function __construct() {
$this->description = "Espresso";
}

public function cost() {
return 1.99;
}
}

class HouseBlend extends Beverage {
public function __construct() {
$this->description = "HouseBlend";
}

public function cost() {
return .89;
}
}

class DarkRoast extends Beverage {
public function __construct() {
$this->description = "DarkRoast";
}

public function cost() {
return .99;
}
}

class Mocha extends CondimentDecorator {
public $beverage;

public function __construct(Beverage $beverage) {
$this->beverage = $beverage;
}
public function getDescription() {
return $this->beverage->getDescription() . ", Mocha";
}
public function cost() {
return .20 + $this->beverage->cost();
}
}

class Whip extends CondimentDecorator {
public $beverage;

public function __construct(Beverage $beverage) {
$this->beverage = $beverage;
}
public function getDescription() {
return $this->beverage->getDescription() . ", Whip";
}
public function cost() {
return .10 + $this->beverage->cost();
}
}

class Soy extends CondimentDecorator {
public $beverage;

public function __construct(Beverage $beverage) {
$this->beverage = $beverage;
}
public function getDescription() {
return $this->beverage->getDescription() . ", Soy";
}
public function cost() {
return .15 + $this->beverage->cost();
}
}

$beverage = new Espresso();
echo $beverage->getDescription() . "\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";
?>

状态模式
复制代码 代码如下:

<?php

class GumballMachine {
const SOLD_OUT = 0;
const NO_QUARTER = 1;
const HAS_QUARTER = 2;
const SOLD = 3;

public $state = self::SOLD_OUT;
public $count = 0;

public function __construct($count) {
$this->count = $count;
if ($count > 0) {
$this->state = self::NO_QUARTER;
}
}

public function insertQuarter() {
if ($this->state == self::HAS_QUARTER) {
echo "You can't insert another quarter!\n";
} else if ($this->state == self::NO_QUARTER) {
$this->state = self::HAS_QUARTER;
echo "You inserted a quarter!\n";
} else if ($this->state == self::SOLD_OUT) {
echo "You can't insert a quarter, the machine is sold out!\n";
} else if ($this->state == self::SOLD) {
echo "Please wait, we're already giving you a gumball!\n";
}
}
}

$obj = new GumballMachine(0);
print_r($obj)

?>

相关文章

  • PHP中功能强大却很少使用的函数实例小结

    PHP中功能强大却很少使用的函数实例小结

    这篇文章主要介绍了PHP中功能强大却很少使用的函数,结合实例形式总结分析了php中非常实用的几个函数,包括函数的调用、注册、调用、判断等操作技巧,需要的朋友可以参考下
    2016-11-11
  • php上传大文件设置方法

    php上传大文件设置方法

    本文给大家分享的是在php中如何设置才能实现大文件上传的方法,十分的实用,有需要的小伙伴可以参考下
    2016-04-04
  • php简单构造json多维数组的方法示例

    php简单构造json多维数组的方法示例

    这篇文章主要介绍了php简单构造json多维数组的方法,结合实例形式分析了php数据库查询结果的json格式转换操作技巧,需要的朋友可以参考下
    2017-06-06
  • PHP中extract()函数的定义和用法

    PHP中extract()函数的定义和用法

    PHP extract() 函数从数组中把变量导入到当前的符号表中
    2012-08-08
  • PHP遍历目录文件的常用方法小结

    PHP遍历目录文件的常用方法小结

    这篇文章主要介绍了PHP遍历目录文件的常用方法,结合实例形式总结分析了php针对文件与目录的常用遍历技巧与运行效率,具有一定参考借鉴价值,需要的朋友可以参考下
    2017-02-02
  • PHP7内核之Reference详解

    PHP7内核之Reference详解

    这篇文章主要介绍了PHP7内核之Reference,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03
  • PHP排序算法类实例

    PHP排序算法类实例

    这篇文章主要介绍了PHP排序算法类,实例分析了插入排序、选择排序、冒泡排序、快速排序等排序算法的原理与实现技巧,需要的朋友可以参考下
    2015-06-06
  • [PHP]实用函数2

    [PHP]实用函数2

    [PHP]实用函数2...
    2007-11-11
  • 如何在PHP中操作临时文件

    如何在PHP中操作临时文件

    关于文件相关的操作,想必大家已经非常了解了,在将来我们刷到手册中相关的文件操作函数时也会进行详细的讲解。今天,我们先来了解一下在 PHP 中关于临时文件相关的一些内容。
    2021-05-05
  • PHP伪协议基本原理介绍

    PHP伪协议基本原理介绍

    这篇文章主要介绍了PHP伪协议,php中有很多封装协议,最常见的如file协议,php协议,data协议,zip和phar协议等等
    2022-11-11

最新评论