PHP设计模式-组合 发表于 2017-03-27 | 分类于 PHP | 评论数: | 阅读次数: 本文字数: 4.3k | 阅读时长 ≈ 4 分钟 适用性 定义:将对象以树形结构组织起来,以达成“部分-整体”的层次结构,使得客户端对单个对象和组合对象的使用具有一致性 我的理解:把对象构建成树形结构 代码示例123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245/** * 根节点和树节点都要实现的接口 */interface CompositeInterface{ /** * 增加一个节点对象 * * @return mixed */ public function add(CompositeInterface $composite); /** * 删除节点一个对象 * * @return mixed */ public function delete(CompositeInterface $composite); /** * 实体类要实现的方法 * * @return mixed */ public function operation(); /** * 打印对象组合 * * @return mixed */ public function printComposite();}/** * 文件实体. */class File implements CompositeInterface{ /** * 文件名称. * * @var string */ private $_name = ''; /** * 文件内容. * * @var string */ private $_content = ''; /** * 构造函数. * * @param string $name */ public function __construct($name = '') { $this->_name = $name; } /** * 魔法函数 * @param string $name 属性名称 * @return mixed */ public function __get($name='') { $name = '_' . $name; return $this->$name; } /** * 增加一个节点对象 * * @return mixed */ public function add(CompositeInterface $composite) { throw new Exception('not support', 500); } /** * 删除节点一个对象 * * @return mixed */ public function delete(CompositeInterface $composite) { throw new Exception('not support', 500); } /** * 打印对象组合. * * @return mixed */ public function printComposite() { throw new Exception('not support', 500); } /** * 实体类要实现的方法. * * @return mixed */ public function operation($operation = '', $content = '') { switch ($operation) { case 'write': $this->_content .= $content; echo 'write success'; break; case 'read': echo $this->_content; break; default: throw new \Exception("not support", 400); break; } }}/** * 文件夹实体 */class Folder implements CompositeInterface{ /** * 对象组合 * @var array */ private $_composite = []; /** * 文件夹名称 * @var string */ private $_name = ''; /** * 构造函数 * * @param string $name */ public function __construct($name='') { $this->_name = $name; } /** * 魔法函数 * @param string $name 属性名称 * @return mixed */ public function __get($name='') { $name = '_' . $name; return $this->$name; } /** * 增加一个节点对象 * * @return void */ public function add(CompositeInterface $composite) { if (in_array($composite, $this->_composite, true)) { return; } $this->_composite[] = $composite; } /** * 删除节点一个对象 * * @return void */ public function delete(CompositeInterface $composite) { $key = array_search($composite, $this->_composite, true); if (!$key) { throw new Exception("not found", 404); } unset($this->_composite[$key]); $this->_composite = array_values($this->_composite); } /** * 打印对象组合 * * @return void */ public function printComposite() { foreach ($this->_composite as $v) { if ($v instanceof Folder) { echo '---' . $v->name . "---\n"; $v->printComposite(); continue; } echo $v->name . "\n"; } } /** * 实体类要实现的方法 * * @return mixed */ public function operation() { return; }}try { // 构建一个根目录 $root = new Folder('根目录'); // 根目录下添加一个test.php的文件和usr,mnt的文件夹 $testFile = new File('test.php'); $usr = new Folder('usr'); $mnt = new Folder('mnt'); $root->add($testFile); $root->add($usr); $root->add($mnt); $usr->add($testFile);// usr目录下加一个test.php的文件 // 打印根目录文件夹节点 $root->printComposite();} catch (\Exception $e) { echo $e->getMessage();}