PHP design pattern series-Resource Library pattern (Repository) 1. Pattern Definition
Repository is an independent layer between the domain layer and the data ing layer (data access layer. Its existence makes the domain layer unable to feel the existence of the data access layer. It provides an interface similar to a set for the domain layer to access domain objects. The Repository is the Repository Administrator. the Repository administrator only needs to tell the Repository administrator what the domain layer needs. the Repository administrator does not need to know where the things are actually put.
The Repository mode is the architecture mode, which has reference value only when designing the architecture. The benefits of applying the Repository mode are much higher than the code added to implementing this mode. This mode should be used as long as the project is layered.
2. UML class diagram
3. Sample code
Post. php
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
Persistence = $ persistence;}/*** return the Post object by specifying the id ** @ 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 ;} /*** save the specified object and return ** @ 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 ;} /*** delete the specified Post object ** @ param Post $ post * @ return bool */public function delete (Post $ post) {return $ this-> persistence-> delete ($ post-> getId ());}}
Storage. php
MemoryStorage. php
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 ;}}