PHP设计模式之迭代器、代理模式

本文章讲述了php设计模式中的迭代器模式和代理模式,以及简单的应用实例

PHP设计模式之迭代器、代理模式

一、迭代器模式

  1. 迭代器模式,在不需要了解内部实现的前提下,遍历一个聚合对象的内部元素
  2. 相比传统的编程模式,迭代器模式可以隐藏遍历元素所需的操作

应用场景

遍历数据库表,拿到所有的user对象,然后用佛 foreach 循环,在循环的过程中修改某些字段的值。

实例代码:

class MySQLis{
    protected $conn;
    public function connect($host, $user, $pwd, $dbname){
        $this->conn = mysqli_connect($host, $user, $pwd, $dbname);
    }
    public function query($sql){
        $res =mysqli_query($this->conn, $sql);
    return $res;
    }
    public function close(){
    mysqli_close($this->conn);
    }
}
class User{//数据对象模式
    public $id;
    public $name;
    public $tell;
    protected $db;
    public function __construct($id){
    $this->db = new MySQLis();
    $this->db->connect('localhost', 'func', 'passwd', 'test');
    $res = $this->db->query("select * from user where id = {$id} limit 1");
    $data = $res->fetch_assoc();
    $this->id = $data['id'];
    }
    public function __destruct(){
    $this->db->query("update user set name = '{$this->name}', tell = '{$this->tell}' where id= {$this->id}");
    }
}
class Factory{//工厂模式
    public static function CreateAnimal($name){
    }
    public static function getUser($id){
    $key = "user_{$id}";
    $user = Register::get($key);
    if (!$user) {
        $user = new User($id);
        Register::set($key, $user);
    }
    return $user;
    }
}
class Register {//注册器模式
    protected static $object;
    public static function set($alias, $object){
    self::$object[$alias] = $object;
    }
    public static function get($alias){
    return self::$object[$alias];
    }
    public static function _unset($alias){
    unset(self::$object[$alias]);
    }
}
class AllUser implements \Iterator{
    protected $ids;
    protected $index;
    protected $data = array();
    public function __construct(){
    $db = new MySQLis();
    $db->connect('localhost', 'func', 'passwd', 'test');
    $res = $db->query('select id from user');
    $this->ids = $res->fetch_all(MYSQLI_ASSOC);
    }
    //返回当前索引游标指向的元素 
    public function  current(){
    $id = $this->ids[$this->index]['id'];
    return Factory::getUser($id);
    }   
    //移动当前索引游标到下一元素  
    public function next(){
    $this->index ++;
    }
    //返回当前索引游标指向的键  
    public function key(){
    return $this->index;
    }
    //判断当前索引游标指向的元素是否有效 
    public function valid(){
    return $this->index < count($this->ids);
    }
    //重置索引游标  
    public function rewind(){
    $this->index = 0;
    }
}
$user = new AllUser();
foreach ($user as $key => $value) {
   print_r($value);
}

二、代理模式

在客户端与实体之间建立一个代理对象(proxy),客户端对实体进行操作全部委派代理对象,隐藏实体的具体实现细节;

proxy还可以与业务代码分离,部署到另外的服务器。业务代码中通过RPC来委派任务

数据库主从,通过代理设置主从读写设置。

示例代码:

class MySQLis{
    protected $conn;
    public function connect($host, $user, $pwd, $dbname){
    $this->conn = mysqli_connect($host, $user, $pwd, $dbname);
    }
    public function query($sql){
    $res =mysqli_query($this->conn, $sql);
    return $res;
    }
    public function close(){
    mysqli_close($this->conn);
    }
}
class Proxy implements IUserProxy{
    public function getUserName($id){
    $db = new MySQLis();
    $db->connect('localhost', 'func', 'passwd', 'test');
    $db->query('select * from user where id='.$id);
    }
    public function setUserName($id, $name){
    $db = new MySQLis();
    $db->connect('localhost', 'func', 'passwd', 'test');
    $db->query("update user set name= '{$name}' where id= {$id}");
    }
}
interface IUserProxy{
    public function getUserName($id);
    public function setUserName($id, $name);
}
$proxy = new Proxy();
$proxy->getUserName(1);
$proxy->setUserName(1, 'admin');
上一篇:PHP设计模式之观察者、原型以及装饰模式 下一篇:整理Yii2中定义的常用路径别名

评论

评论(0)

暂无评论