-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathDelegate.class.php
executable file
·122 lines (93 loc) · 3.02 KB
/
Delegate.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?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;