功能:
显示用户及该用户推广进来的下属两级用户
一.User类
/** * Class User */class User{ private $uid; private $name; private $level; public function __construct($uid, $name, $level) { $this->uid = $uid; $this->name = $name; $this->level = $level; } public function getName() { return $this->name; } public function getUid() { return $this->uid; }}
二.User类的装饰类(装饰类主要实现对User对象信息的更友好的显示)
/** * User类的装饰类 * Class UserDecorator */class UserDecorator{ protected $user; public function __construct(User $user) { $this->user = $user; } public function getDisplayHtml($deep) { return ''.str_repeat(' ', ($deep-1) * 2) . "用户名:{$this->user->getName()};UID:{$this->user->getUid()}" .''; }}
三.组合模式
1.抽象构件
/** * 抽象构件 * Class PopUserComponent */abstract class PopUserComponent{ protected $user; public function __construct(UserDecorator $user) { $this->user = $user; } abstract public function add(PopUserComponent $user); abstract public function remove(PopUserComponent $user); abstract public function display($deep);}
2.一级用户:枝节点
/** * 一级用户:枝节点 * Class FirstLevelPopUsers */class FirstLevelPopUsers extends PopUserComponent{ protected $subordinateUsers = []; public function add(PopUserComponent $user) { $this->subordinateUsers[] = $user; } public function remove(PopUserComponent $user) { unset($this->subordinateUsers[array_search($user, $this->subordinateUsers)]); } public function display($deep) { $html = ''; $html .= $this->user->getDisplayHtml($deep); $deep += 2; foreach ($this->subordinateUsers as $eachSubordinateUser) { $html .= $eachSubordinateUser->display($deep); } return $html; }}
3.二级用户:叶节点
/** * 二级用户:叶节点 * Class SecondLevelPopUsers */class SecondLevelPopUsers extends PopUserComponent{ public function add(PopUserComponent $user) { throw new \Exception('不支持该方法'); } public function remove(PopUserComponent $user) { throw new \Exception('不支持该方法'); } public function display($deep) { return $this->user->getDisplayHtml($deep); }}
四.迭代器
/** * 迭代器 * Class PopUserIterator */class PopUserIterator implements Iterator{ private $users = []; private $valid = false; public function __construct() { /** * 此处模拟从数据库读取记录 */ $firstUser = new FirstLevelPopUsers(new UserDecorator(new User(1, '一级用户1', 1))); $firstUser->add(new SecondLevelPopUsers(new UserDecorator(new User(2, '二级用户3', 2)))); $firstUser->add(new SecondLevelPopUsers(new UserDecorator(new User(3, '二级用户4', 2)))); $firstUser->add(new SecondLevelPopUsers(new UserDecorator(new User(4, '二级用户5', 2)))); $this->users[] = $firstUser; $firstUser = new FirstLevelPopUsers(new UserDecorator(new User(6, '一级用户6', 1))); $firstUser->add(new SecondLevelPopUsers(new UserDecorator(new User(7, '二级用户7', 2)))); $firstUser->add(new SecondLevelPopUsers(new UserDecorator(new User(8, '二级用户8', 2)))); $firstUser->add(new SecondLevelPopUsers(new UserDecorator(new User(9, '二级用户9', 2)))); $this->users[] = $firstUser; } public function current() { return current($this->users); } public function next() { $this->valid = (next($this->users) === false) ? false : true; } public function key() { return key($this->users); } public function valid() { return $this->valid; } public function rewind() { $this->valid = (reset($this->users) === false) ? false : true; }}
五.调用
$totalInfo = '';$html = '
输出: