zoukankan      html  css  js  c++  java
  • 组合模式 + 装饰模式 + 迭代器模式 显示两级用户体系

    功能:

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

    一.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 '<tr><td>'.str_repeat('&nbsp', ($deep-1) * 2) . "用户名:{$this->user->getName()};UID:{$this->user->getUid()}" .'</td></tr>';
        }
    }
    

      

    三.组合模式

    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   =  '<table border="1" bgcolor="#faebd7">';
    
    $popUserIterator = new PopUserIterator();
    foreach ($popUserIterator as $val) {
        $html .= $val->display(1);
    }
    
    $html   .= '</table>';
    
    echo $html;
    

      

    输出:

  • 相关阅读:
    注册接口
    配置node 的路由
    mongoose 与 mylab 的使用 (1)
    PHP中PDO错误/异常(PDOException)处理
    如何使用PDO查询Mysql来避免SQL注入风险?ThinkPHP 3.1中的SQL注入漏洞分析!
    PHP5中PDO的入门教程
    PHP文件夹文件拷贝/复制函数 dir_copy($src = '', $dst = '')
    php将图片以二进制保存到mysql数据库并显示
    php 显示sqlserver二进制图片
    XML文档的PHP程序查询代码
  • 原文地址:https://www.cnblogs.com/itfenqing/p/8734082.html
Copyright © 2011-2022 走看看