php 代码均来源于《PHP设计模式》一书(3)
Interpreter.class.phpnbsp;~nbsp;2KBnbsp;nbsp;nbsp;nbsp;下载(20) ?php/** * 转自 《PHP设计模式》 第十章: 解释器模式 * 解释器: 解释器设计模式用于分析一个实体的关
Interpreter.class.php ~ 2KB 下载(20)
<?php /** * 转自 《PHP设计模式》 第十章: 解释器模式 * 解释器: 解释器设计模式用于分析一个实体的关键元素,并且针对每个元素都提供自己的解释或相应的动作。 * 解释器设计模式最常用于PHP/HTML 模板系统。 * */ class User { protected $_username = ""; public function __construct($username) { $this->_username = $username; } public function getProfilePage() { $profile = "<h2>I like Never Again ! </h2>"; $profile .= "I love all of their songs. My favorite CD: <br />"; $profile .= "{{myCD.getTitle}}!!"; return $profile; } } class userCD { protected $_user = NULL; public function setUser(User $user) { $this->_user = $user; } public function getTitle() { $title = "Waste of a Rib"; return $title; } } class userCDInterpreter { protected $_user = NULL; public function setUser(User $user) { $this->_user = $user; } public function getInterpreted() { $profile = $this->_user->getProfilePage(); if (preg_match_all('/\{\{myCD\.(.*?)\}\}/', $profile, $triggers, PREG_SET_ORDER)) { $replacements = array(); foreach ($triggers as $trigger) { $replacements[] = $trigger[1]; } $replacements = array_unique($replacements); $myCD = new userCD(); $myCD->setUser($this->_user); foreach ($replacements as $replacement) { $profile = str_replace("{{myCD.{$replacement}}}", call_user_func(array($myCD, $replacement)), $profile); } } return $profile; } } $username = "aaron"; $user = new User($username); $interpreter = new userCDInterpreter(); $interpreter->setUser($user); print "<h1>{$username}'s Profile</h1>"; print $interpreter->getInterpreted(); /* End of Interpreter.class.php */ /* Location the file Design/Interpreter.class.php */
Iterator.class.php ~ 2KB 下载(19)
<?php /** * 转自 《PHP设计模式》 第十一章: 迭代器模式 * 迭代器:迭代器设计模式可帮助构造特定对象, 那些对象能够提供单一标准接口循环或迭代任何类型的可计数数据。 * 处理需要遍历的可计数数据时, 最佳的解决办法是创建一个基于迭代器设计模式的对象。 * */ class CD { public $band = ""; public $title = ""; public $trackList = array(); public function __construct($band, $title) { $this->band = $band; $this->title = $title; } public function addTrack($track) { $this->trackList[] = $track; } } class CDSearchByBandIterator implements Iterator { private $_CDs = array(); private $_valid = FALSE; public function __construct($bandName) { $db = mysql_connect("localhost", "root", "root"); mysql_select_db("test"); $sql = "select CD.id, CD.band, CD.title, tracks.tracknum, tracks.title as tracktitle "; $sql .= "from CD left join tracks on CD.id = tracks.cid "; $sql .= "where band = '" . mysql_real_escape_string($bandName) . "' "; $sql .= "order by tracks.tracknum"; $results = mysql_query($sql); $cdID = 0; $cd = NULL; while ($result = mysql_fetch_array($results)) { if ($result["id"] !== $cdID) { if ( ! is_null($cd)) { $this->_CDs[] = $cd; } $cdID = $result['id']; $cd = new CD($result['band'], $result['title']); } $cd->addTrack($result['tracktitle']); } $this->_CDs[] = $cd; } public function next() { $this->_valid = (next($this->_CDs) === FALSE) ? FALSE : TRUE; } public function rewind() { $this->_valid = (reset($this->_CDs) === FALSE) ? FALSE : TRUE; } public function valid() { return $this->_valid; } public function current() { return current($this->_CDs); } public function key() { return key($this->_CDs); } } $queryItem = "Never Again"; $cds = new CDSearchByBandIterator($queryItem); print "<h1>Found the Following CDs</h1>"; print "<table border='1'><tr><th>Band</th><th>Ttile</th><th>Num Tracks</th></tr>"; foreach ($cds as $cd) { print "<tr><td>{$cd->band}</td><td>{$cd->title}</td><td>"; print count($cd->trackList). "</td></tr>"; } print "</table>"; /* End of Iterator.class.php */ /* Location the file Design/Iterator.class.php */
- 上一篇:Nginx 使用nginx和php实时产生缩略图
- 下一篇:php 简单计算器
精彩图集
精彩文章