《Head First 設(shè)計(jì)模式》代碼之PHP版(面向?qū)ο髮W(xué)習(xí))
更新時(shí)間:2010年06月27日 01:34:54 作者:
《Head First 設(shè)計(jì)模式》是本不錯(cuò)的講解設(shè)計(jì)模式的書(shū),不像F4寫的那么枯燥,應(yīng)該算是比較容易理解的好書(shū)。
觀察者模式
復(fù)制代碼 代碼如下:
<?php
/**
* 觀察者模式
* 定義了對(duì)象之間的一對(duì)多依賴,當(dāng)一個(gè)對(duì)象改變狀態(tài)時(shí),
* 它的所有依賴者都會(huì)收到通知并自動(dòng)更新。
*/
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);
?>
命令模式
復(fù)制代碼 代碼如下:
<?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();
?>
裝飾者模式
復(fù)制代碼 代碼如下:
<?php
/**
* 裝飾著模式
* 動(dòng)態(tài)地將責(zé)任附加到對(duì)象上,若要擴(kuò)展功能,裝飾者提供了比繼承更有彈性的替代方案。
*/
abstract class Beverage {
public $description = "Unknown Beverage";
public function getDescription() {
return $this->description;
}
public abstract function cost();
}
abstract class CondimentDecorator extends Beverage {
//JAVA代碼里這里是個(gè)抽象類,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";
?>
狀態(tài)模式
復(fù)制代碼 代碼如下:
<?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面向?qū)ο蟪绦蛟O(shè)計(jì)組合模式與裝飾模式詳解
- php面向?qū)ο笾祮卫J?/a>
- 深入php面向?qū)ο?、模式與實(shí)踐
- PHP 面向?qū)ο蟪绦蛟O(shè)計(jì)(oop)學(xué)習(xí)筆記(三) - 單例模式和工廠模式
- PHP面向?qū)ο髮W(xué)習(xí)筆記之二 生成對(duì)象的設(shè)計(jì)模式
- 不錯(cuò)的一篇面向?qū)ο蟮腜HP開(kāi)發(fā)模式(簡(jiǎn)寫版)
- 學(xué)習(xí)php設(shè)計(jì)模式 php實(shí)現(xiàn)抽象工廠模式
- PHP實(shí)現(xiàn)設(shè)計(jì)模式中的抽象工廠模式詳解
- 初次接觸php抽象工廠模式(Elgg)
- php單例模式實(shí)現(xiàn)(對(duì)象只被創(chuàng)建一次)
- PHP實(shí)現(xiàn)單例模式最安全的做法
- PHP面向?qū)ο蟪绦蛟O(shè)計(jì)之對(duì)象生成方法詳解
相關(guān)文章
PHP中功能強(qiáng)大卻很少使用的函數(shù)實(shí)例小結(jié)
這篇文章主要介紹了PHP中功能強(qiáng)大卻很少使用的函數(shù),結(jié)合實(shí)例形式總結(jié)分析了php中非常實(shí)用的幾個(gè)函數(shù),包括函數(shù)的調(diào)用、注冊(cè)、調(diào)用、判斷等操作技巧,需要的朋友可以參考下2016-11-11php簡(jiǎn)單構(gòu)造json多維數(shù)組的方法示例
這篇文章主要介紹了php簡(jiǎn)單構(gòu)造json多維數(shù)組的方法,結(jié)合實(shí)例形式分析了php數(shù)據(jù)庫(kù)查詢結(jié)果的json格式轉(zhuǎn)換操作技巧,需要的朋友可以參考下2017-06-06