PHP 设计模式系列 —— 资源库模式(Repository)
生活随笔
收集整理的這篇文章主要介紹了
PHP 设计模式系列 —— 资源库模式(Repository)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、模式定義
Repository 是一個獨立的層,介于領域層與數據映射層(數據訪問層)之間。它的存在讓領域層感覺不到數據訪問層的存在,它提供一個類似集合的接口提供給領域層進行領域對象的訪問。Repository 是倉庫管理員,領域層需要什么東西只需告訴倉庫管理員,由倉庫管理員把東西拿給它,并不需要知道東西實際放在哪。
Repository 模式是架構模式,在設計架構時,才有參考價值。應用 Repository 模式所帶來的好處,遠高于實現這個模式所增加的代碼。只要項目分層,都應當使用這個模式。
2、UML類圖
3、示例代碼
Post.php
<?phpnamespace DesignPatterns\More\Repository;/** * Post 類 * @package DesignPatterns\Repository */ class Post { /** * @var int */ private $id; /** * @var string */ private $title; /** * @var string */ private $text; /** * @var string */ private $author; /** * @var \DateTime */ private $created; /** * @param int $id */ public function setId($id) { $this->id = $id; } /** * @return int */ public function getId() { return $this->id; } /** * @param string $author */ public function setAuthor($author) { $this->author = $author; } /** * @return string */ public function getAuthor() { return $this->author; } /** * @param \DateTime $created */ public function setCreated($created) { $this->created = $created; } /** * @return \DateTime */ public function getCreated() { return $this->created; } /** * @param string $text */ public function setText($text) { $this->text = $text; } /** * @return string */ public function getText() { return $this->text; } /** * @param string $title */ public function setTitle($title) { $this->title = $title; } /** * @return string */ public function getTitle() { return $this->title; } }PostRepository.php
<?phpnamespace DesignPatterns\More\Repository;use DesignPatterns\More\Repository\Storage; /** * Post 對應的 Repository * 該類介于數據實體層(Post) 和訪問對象層(Storage)之間 * * Repository 封裝了持久化對象到數據存儲器以及在展示層顯示面向對象的視圖操作 * * Repository 還實現了領域層和數據映射層的分離和單向依賴 * * PostRepository 類 * @package DesignPatterns\Repository */ class PostRepository { private $persistence; public function __construct(Storage $persistence) { $this->persistence = $persistence; } /** * 通過指定id返回Post對象 * * @param int $id * @return Post|null */ public function getById($id) { $arrayData = $this->persistence->retrieve($id); if (is_null($arrayData)) { return null; } $post = new Post(); $post->setId($arrayData['id']); $post->setAuthor($arrayData['author']); $post->setCreated($arrayData['created']); $post->setText($arrayData['text']); $post->setTitle($arrayData['title']); return $post; } /** * 保存指定對象并返回 * * @param Post $post * @return Post */ public function save(Post $post) { $id = $this->persistence->persist(array( 'author' => $post->getAuthor(), 'created' => $post->getCreated(), 'text' => $post->getText(), 'title' => $post->getTitle() )); $post->setId($id); return $post; } /** * 刪除指定的 Post 對象 * * @param Post $post * @return bool */ public function delete(Post $post) { return $this->persistence->delete($post->getId()); } }Storage.php
<?phpnamespace DesignPatterns\More\Repository;/** * Storage接口 * * 該接口定義了訪問數據存儲器的方法 * 具體的實現可以是多樣化的,比如內存、關系型數據庫、NoSQL數據庫等等 * * @package DesignPatterns\Repository */ interface Storage { /** * 持久化數據方法 * 返回新創建的對象ID * * @param array() $data * @return int */ public function persist($data); /** * 通過指定id返回數據 * 如果為空返回null * * @param int $id * @return array|null */ public function retrieve($id); /** * 通過指定id刪除數據 * 如果數據不存在返回false,否則如果刪除成功返回true * * @param int $id * @return bool */ public function delete($id); }MemoryStorage.php
<?phpnamespace DesignPatterns\More\Repository;use DesignPatterns\More\Repository\Storage; /** * MemoryStorage類 * @package DesignPatterns\Repository */ class MemoryStorage implements Storage { private $data; private $lastId; public function __construct() { $this->data = array(); $this->lastId = 0; } /** * {@inheritdoc} */ public function persist($data) { $this->data[++$this->lastId] = $data; return $this->lastId; } /** * {@inheritdoc} */ public function retrieve($id) { return isset($this->data[$id]) ? $this->data[$id] : null; } /** * {@inheritdoc} */ public function delete($id) { if (!isset($this->data[$id])) { return false; } $this->data[$id] = null; unset($this->data[$id]); return true; } }轉載于:https://www.cnblogs.com/wntd/p/8980862.html
總結
以上是生活随笔為你收集整理的PHP 设计模式系列 —— 资源库模式(Repository)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux期中架构
- 下一篇: 阿里资深技术专家:在各阶段中,3年经验的