博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
组合模式 + 装饰模式 + 迭代器模式 显示两级用户体系
阅读量:5821 次
发布时间:2019-06-18

本文共 3920 字,大约阅读时间需要 13 分钟。

功能:

显示用户及该用户推广进来的下属两级用户

一.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   =  '
';$popUserIterator = new PopUserIterator();foreach ($popUserIterator as $val) { $html .= $val->display(1);}$html .= '
';echo $html;

  

输出:

 

转载于:https://www.cnblogs.com/itfenqing/p/8734082.html

你可能感兴趣的文章
【年终总结】微信前端社招有感
查看>>
(C#)Windows Shell 外壳编程系列6 - 执行
查看>>
boost多边形交集、并集
查看>>
ifconfig
查看>>
理解字符编码
查看>>
List of colors
查看>>
POS打印机一些特殊控制
查看>>
datasnap生命期LifeCycle
查看>>
TCPDUMP Command Examples
查看>>
Redis3.2+Tomcat实现集群的Session管理 -- tomcat-redis-session-manager的编译和开发部署环境搭建...
查看>>
数据集和JSON相互转换
查看>>
Without you I am NOTHING
查看>>
深入浅出话VC++(1)——Windows程序内部运行机制
查看>>
BAT等互联网公司薪资分享
查看>>
Eclipse插件开发(原书第3版)
查看>>
街头智慧:罗杰斯的投资与人生
查看>>
《社会化产品经理:社会化商业时代的产品管理与设计》
查看>>
Ionic 简单操作
查看>>
POJ 2396 Budget(有上下限的最大流)
查看>>
openOJ 1897 Forbidden Words(自动机)
查看>>