html5中文学习网

您的位置: 首页 > 网络编程 > PHP编程 » 正文

PHP设计模式之委托模式_PHP教程_编程技术

[ ] 已经帮助:人解决问题

当一个对象包含复杂单独立的,必须基于判决执行的功能性的若干部分时,最佳的方法是适用基于委托设计模式的对象。 7xRHTML5中文学习网 - HTML5先行者学习网

  1. <?php    
  2.     /**  
  3.      * 示例: Web站点具有创建MP3文件播放列表的功能, 也具有选择以 M3U 或 PLS 格式下载播放列表的功能。  
  4.      * 以下代码示例展示常规与委托两种模式实现  
  5.      */   
  6.     //常规实现    
  7.     class Playlist {   
  8.            
  9.         private $_songs;    
  10.            
  11.         public function __construct() {   
  12.             $this->_songs = array();   
  13.         }   
  14.            
  15.         public function addSong($location$title) {   
  16.             $song = array("location" => $location"title" => $title);   
  17.             $this->_songs[] = $song;   
  18.         }   
  19.            
  20.         public function getM3U() {   
  21.             $m3u = "#EXTM3U/n/n";   
  22.                
  23.             foreach ($this->_songs as $song) {   
  24.                 $m3u .= "#EXTINF: -1, {$song['title']}/n";   
  25.                 $m3u .= "{$song['location']}/n";   
  26.             }   
  27.                
  28.             return $m3u;   
  29.         }   
  30.            
  31.         public function getPLS() {   
  32.             $pls = "[playlist]]/nNumberOfEntries = "count($this->_songs) . "/n/n";   
  33.                
  34.             foreach ($this->_songs as $songCount => $song) {   
  35.                 $counter = $songCount + 1;   
  36.                 $pls .= "File{$counter} = {$song['location']}/n";   
  37.                 $pls .= "Title{$counter} = {$song['title']}/n";   
  38.                 $pls .= "LengthP{$counter} = -1 /n/n";   
  39.             }   
  40.                
  41.             return $pls;   
  42.         }   
  43.     }   
  44.        
  45.     $playlist = new Playlist();   
  46.        
  47.     $playlist->addSong("/home/aaron/music/brr.mp3""Brr");   
  48.     $playlist->addSong("/home/aaron/music/goodbye.mp3""Goodbye");   
  49.        
  50.     $externalRetrievedType = "pls";   
  51.        
  52.     if ($externalRetrievedType == "pls") {   
  53.         $playlistContent =  $playlist->getPLS();   
  54.     } else {   
  55.         $playlistContent =  $playlist->getM3U();   
  56.     }   
  57.        
  58.     echo $playlistContent;   
  59.        
  60.     //委托模式实现    
  61.     class newPlaylist {   
  62.        
  63.         private $_songs;   
  64.         private $_tyepObject;   
  65.        
  66.         public function __construct($type) {   
  67.             $this->_songs = array();   
  68.             $object = "{$type}Playlist";   
  69.             $this->_tyepObject = new $object;   
  70.         }      
  71.            
  72.         public function addSong($location$title) {   
  73.             $song = array("location" => $location"title" => $title);   
  74.             $this->_songs[] = $song;   
  75.         }   
  76.            
  77.         public function getPlaylist() {   
  78.             $playlist = $this->_tyepObject->getPlaylist($this->_songs);   
  79.             return $playlist;   
  80.         }   
  81.     }   
  82.        
  83.     class m3uPlaylist {   
  84.         public function getPlaylist($songs) {   
  85.             $m3u = "#EXTM3U/n/n";   
  86.                
  87.             foreach ($songs as $song) {   
  88.                 $m3u .= "#EXTINF: -1, {$song['title']}/n";   
  89.                 $m3u .= "{$song['location']}/n";   
  90.             }   
  91.                
  92.             return $m3u;   
  93.         }      
  94.     }   
  95.        
  96.     class plsPlaylist {   
  97.         public function getPlaylist($songs) {   
  98.             $pls = "[playlist]]/nNumberOfEntries = "count($songs) . "/n/n";   
  99.                
  100.             foreach ($songs as $songCount => $song) {   
  101.                 $counter = $songCount + 1;   
  102.                 $pls .= "File{$counter} = {$song['location']}/n";   
  103.                 $pls .= "Title{$counter} = {$song['title']}/n";   
  104.                 $pls .= "LengthP{$counter} = -1 /n/n";   
  105.             }   
  106.                
  107.             return $pls;   
  108.         }   
  109.     }   
  110.        
  111.     $externalRetrievedType = "pls";   
  112.     $playlist = new newPlaylist($externalRetrievedType);   
  113.        
  114.     $playlist->addSong("/home/aaron/music/brr.mp3""Brr");   
  115.     $playlist->addSong("/home/aaron/music/goodbye.mp3""Goodbye");   
  116.        
  117.     $playlistContent = $playlist->getPlaylist();   
  118.     echo $playlistContent;   
  119.  ?> 

 数据库脚本请参照:http://www.cxybl.com/html/wlbc/Php/2011_1126_9458.html7xRHTML5中文学习网 - HTML5先行者学习网

7xRHTML5中文学习网 - HTML5先行者学习网
7xRHTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助