PHP设计模式-仓库模式 Repository

PHP仓库设计模式(Repository)是一个位于领域层(Domain Layer)和数据映射层(数据访问层)之间的中介层,它使用类似集合仓库的接口来访问领域对象。Repository 仓库封装了持久化存储在数据库中的对象的集合和对它们的操作,为持久层提供了一种更加面向对象的方式。Repository 同样对应用领域层和数据映射层之间的简洁分离和单向依赖有很大的帮助。

比如这些PHP框架Doctrine 2 的ORM模块: 在Entity 和 DBAL之间就有仓库层调停两者关系并包含获得对象的方法。其他如Laravel框架等。下面是MemoryStorage和Post数据映射层之间的Repository的PHP代码实例:

// 存储接口
interface StorageInterface
{
    // 持久化数据的方法,返回刚持久化的数据id
    public function persist($data);


    // 获取相应id的数据
    public function retrieve($id);

    // 删除相应id的数据,没有该id返回false,删除成功返回true
    public function delete($id);
}
// Domian 领域层-内存存储
class MemoryStorage implements StorageInterface
{
    private $data;
    private $lastId;

    public function __construct()
    {
        $this->data = array();
        $this->lastId = 0;
    }

    public function persist($data)
    {
        $this->data[++$this->lastId] = $data;
        return $this->lastId;
    }

    public function retrieve($id)
    {
        return isset($this->data[$id]) ? $this->data[$id] : null;
    }

    public function delete($id)
    {
        if(!isset($this->data[$id])){
            return false;
        }

        $this->data[$id] = null;
        unset($this->data[$id]);

        return true;
    }
}
// Post数据映射层对象
class Post
{
    private $id;
    private $title;
    private $author;
    private $created;

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getId()
    {
        return $this->id;
    }

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setAuthor($author)
    {
        $this->author = $author;
    }

    public function getAuthor()
    {
        return $this->author;
    }

    // DateTime $created
    public function setCreated($created)
    {
        $this->created = $created;
    }

    public function getCreated()
    {
        return $this->created;
    }
}
// 仓库设计模式中介,包含与上面两个类对象处理的方法
class Repository
{
    private $persistence;

    public function __construct(StorageInterface $persistence)
    {
        $this->persistence = $persistence;
    }

    // 根据指定ID返回Post对象
    public function getById($id)
    {
        $arrayData = $this->persistence->retrieve($id);
        if(is_null($arrayData)){
            return null;
        }

        $post = new Post();
        $post->setId($arrayData['id']);
        $post->setTitle($arrayData['title']);
        $post->setAuthor($arrayData['author']);
        $post->setCreated($arrayData['created']);

        return $post;
    }

    // 保存Post对象并填充ID
    public function save(Post $post)
    {
        $id = $this->persistence->persist(array(
            'title' => $post->getTitle()
            'author' => $post->getAuthor(),
            'created' => $post->getCreated(),
        ));

        $post->setId($id);
        return $post;
    }

    // 删除指定的Post 对象
    public function delete(Post $post)
    {
        return $this->persistence->delete($post->getId());
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *