php 代码均来源于《PHP设计模式》一书
代码均来源于《PHP设计模式》一书 代码均来源于《PHP设计模式》一书**Decorator.class.phpamp;nbsp;~amp;nbsp;2KBamp;nbsp;amp;nbsp;amp;nbsp;amp;nbsp;下载(35)**[代码片段(72行)]**Delegate.class.phpamp;nbsp;~amp;nbsp
代码均来源于《PHP设计模式》一书
Decorator.class.php ~ 2KB 下载(35)
<?php
/**
* 转自 《PHP设计模式》 第六章: 装饰器模式
*
* 装饰器设计模式适用于下列工作场合: 需求变化是快速和细小的,而且几乎不影响应用程序的其他部分。()
* 使用装饰器设计模式设计类的目标是: 不必重写任何已有的功能性代码,而是对某个基于对象应用增量变化。
* 装饰器设计模式采用这样的构建方式: 在主代码流中应该能够直接插入一个或多个更改或“装饰”目标对象的装饰器,同时不影响其他代码流。
*
*/
class CD {
public $trackList;
public function __construct() {
$this->trackList = array();
}
public function addTrack($track) {
$this->trackList[] = $track;
}
public function getTrackList() {
$output = '';
foreach ($this->trackList as $num => $track) {
$output .= ($num + 1) . ") {$track}.";
}
return $output;
}
}
$tracksFroExternalSource = array("What It Means", "Brr", "Goodbye");
$myCD = new CD();
foreach ($tracksFroExternalSource as $track) {
$myCD->addTrack($track);
}
print "The CD contains:{$myCD->getTrackList()}\n";
/**
* 需求发生小变化: 要求每个输出的参数都采用大写形式. 对于这么小的变化而言, 最佳的做法并非修改基类或创建父 - 子关系,
而是创建一个基于装饰器设计模式的对象。
*
*/
class CDTrackListDecoratorCaps {
private $_cd;
public function __construct(CD $cd) {
$this->_cd = $cd;
}
public function makeCaps() {
foreach ($this->_cd->trackList as & $track) {
$track = strtoupper($track);
}
}
}
$myCD = new CD();
foreach ($tracksFroExternalSource as $track) {
$myCD->addTrack($track);
}
//新增以下代码实现输出参数采用大写形式
$myCDCaps = new CDTrackListDecoratorCaps($myCD);
$myCDCaps->makeCaps();
print "The CD contains:{$myCD->getTrackList()}\n";
/* End of Decorator.class.php */
/* Location the file Design/Decorator.class.php */
Delegate.class.php ~ 3KB 下载(25)
<?php
/**
* 转自 《PHP设计模式》 第七章: 委托模式
* 当一个对象包含复杂单独立的,必须基于判决执行的功能性的若干部分时,最佳的方法是适用基于委托设计模式的对象。
*
*/
/**
* 示例: Web站点具有创建MP3文件播放列表的功能, 也具有选择以 M3U 或 PLS 格式下载播放列表的功能。
*
* 以下代码示例展示常规与委托两种模式实现
*
*/
//常规实现
class Playlist {
private $_songs;
public function __construct() {
$this->_songs = array();
}
public function addSong($location, $title) {
$song = array("location" => $location, "title" => $title);
$this->_songs[] = $song;
}
public function getM3U() {
$m3u = "#EXTM3U\n\n";
foreach ($this->_songs as $song) {
$m3u .= "#EXTINF: -1, {$song['title']}\n";
$m3u .= "{$song['location']}\n";
}
return $m3u;
}
public function getPLS() {
$pls = "[playlist]]\nNumberOfEntries = ". count($this->_songs) . "\n\n";
foreach ($this->_songs as $songCount => $song) {
$counter = $songCount + 1;
$pls .= "File{$counter} = {$song['location']}\n";
$pls .= "Title{$counter} = {$song['title']}\n";
$pls .= "LengthP{$counter} = -1 \n\n";
}
return $pls;
}
}
$playlist = new Playlist();
$playlist->addSong("/home/aaron/music/brr.mp3", "Brr");
$playlist->addSong("/home/aaron/music/goodbye.mp3", "Goodbye");
$externalRetrievedType = "pls";
if ($externalRetrievedType == "pls") {
$playlistContent = $playlist->getPLS();
} else {
$playlistContent = $playlist->getM3U();
}
echo $playlistContent;
//委托模式实现
class newPlaylist {
private $_songs;
private $_tyepObject;
public function __construct($type) {
$this->_songs = array();
$object = "{$type}Playlist";
$this->_tyepObject = new $object;
}
public function addSong($location, $title) {
$song = array("location" => $location, "title" => $title);
$this->_songs[] = $song;
}
public function getPlaylist() {
$playlist = $this->_tyepObject->getPlaylist($this->_songs);
return $playlist;
}
}
class m3uPlaylist {
public function getPlaylist($songs) {
$m3u = "#EXTM3U\n\n";
foreach ($songs as $song) {
$m3u .= "#EXTINF: -1, {$song['title']}\n";
$m3u .= "{$song['location']}\n";
}
return $m3u;
}
}
class plsPlaylist {
public function getPlaylist($songs) {
$pls = "[playlist]]\nNumberOfEntries = ". count($songs) . "\n\n";
foreach ($songs as $songCount => $song) {
$counter = $songCount + 1;
$pls .= "File{$counter} = {$song['location']}\n";
$pls .= "Title{$counter} = {$song['title']}\n";
$pls .= "LengthP{$counter} = -1 \n\n";
}
return $pls;
}
}
$externalRetrievedType = "pls";
$playlist = new newPlaylist($externalRetrievedType);
$playlist->addSong("/home/aaron/music/brr.mp3", "Brr");
$playlist->addSong("/home/aaron/music/goodbye.mp3", "Goodbye");
$playlistContent = $playlist->getPlaylist();
echo $playlistContent;
/* End of Delegate.class.php */
/* Location the file Design/Delegate.class.php */
- 上一篇:Nginx 使用nginx和php实时产生缩略图
- 下一篇:php 简单计算器
精彩图集
精彩文章






