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());
    }
}

留言

這個網誌中的熱門文章

c語言-關於#define用法

CMD常用網管指令

PHP 與 JavaScript 之間傳值利用 json