目的
該模式通過提供集合風格的接口來訪問領域對象,從而協調領域和數據映射層。 資料庫模式封裝了一組存儲在數據存儲器裏的對象和操作它們的方面,這樣子爲數據持久化層提供了更加面向對象的視角。資料庫模式同時也達到了領域層與數據映射層之間清晰分離,單向依賴的目的。
例子
Laravel 框架
Doctrine 2 ORM: 通過資料庫協調實體和 DBAL,它包含檢索對象的方法。
UML圖
代碼
- Post.php
<?php
namespace DesignPatterns\More\Repository;
class Post
{
/**
* @var int|null
*/
private $id;
/**
* @var string
*/
private $title;
/**
* @var string
*/
private $text;
public static function fromState(array $state): Post
{
return new self(
$state['id'],
$state['title'],
$state['text']
);
}
/**
* @param int|null $id
* @param string $text
* @param string $title
*/
public function __construct($id, string $title, string $text)
{
$this->id = $id;
$this->text = $text;
$this->title = $title;
}
public function setId(int $id)
{
$this->id = $id;
}
public function getId(): int
{
return $this->id;
}
public function getText(): string
{
return $this->text;
}
public function getTitle(): string
{
return $this->title;
}
}
- PostRepository.php
<?php
namespace DesignPatterns\More\Repository;
/**
* 這個類位於實體層(Post 類)和訪問對象層(內存)之間。
*
* 資源庫封裝了存儲在數據存儲中的對象集以及他們的操作執行
* 爲持久層提供更加面向對象的視圖
*
* 在域和數據映射層之間,資源庫還支持實現完全分離和單向依賴的目標。
*
*/
class PostRepository
{
/**
* @var MemoryStorage
*/
private $persistence;
public function __construct(MemoryStorage $persistence)
{
$this->persistence = $persistence;
}
public function findById(int $id): Post
{
$arrayData = $this->persistence->retrieve($id);
if (is_null($arrayData)) {
throw new \InvalidArgumentException(sprintf('Post with ID %d does not exist', $id));
}
return Post::fromState($arrayData);
}
public function save(Post $post)
{
$id = $this->persistence->persist([
'text' => $post->getText(),
'title' => $post->getTitle(),
]);
$post->setId($id);
}
}
- MemoryStorage.php
<?php
namespace DesignPatterns\More\Repository;
class MemoryStorage
{
/**
* @var array
*/
private $data = [];
/**
* @var int
*/
private $lastId = 0;
public function persist(array $data): int
{
$this->lastId++;
$data['id'] = $this->lastId;
$this->data[$this->lastId] = $data;
return $this->lastId;
}
public function retrieve(int $id): array
{
if (!isset($this->data[$id])) {
throw new \OutOfRangeException(sprintf('No data found for ID %d', $id));
}
return $this->data[$id];
}
public function delete(int $id)
{
if (!isset($this->data[$id])) {
throw new \OutOfRangeException(sprintf('No data found for ID %d', $id));
}
unset($this->data[$id]);
}
}
測試
- Tests/RepositoryTest.php
<?php
namespace DesignPatterns\More\Repository\Tests;
use DesignPatterns\More\Repository\MemoryStorage;
use DesignPatterns\More\Repository\Post;
use DesignPatterns\More\Repository\PostRepository;
use PHPUnit\Framework\TestCase;
class RepositoryTest extends TestCase
{
public function testCanPersistAndFindPost()
{
$repository = new PostRepository(new MemoryStorage());
$post = new Post(null, 'Repository Pattern', 'Design Patterns PHP');
$repository->save($post);
$this->assertEquals(1, $post->getId());
$this->assertEquals($post->getId(), $repository->findById(1)->getId());
}
}
PHP 互聯網架構師 50K 成長指南+行業問題解決總綱(持續更新)
面試10家公司,收穫9個offer,2020年PHP 面試問題
★如果喜歡我的文章,想與更多資深開發者一起交流學習的話,獲取更多大廠面試相關技術諮詢和指導,歡迎加入我們的羣啊,暗號:phpzh
內容不錯的話希望大家支持鼓勵下點個贊/喜歡,歡迎一起來交流;另外如果有什麼問題 建議 想看的內容可以在評論提出