PHP design mode-Repository resource library mode learning notes-PHP source code

Source: Internet
Author: User
This article introduces you to the Repository resource library mode study notes of the PHP design mode. I hope this article will help you as follows. This article introduces you to the Repository resource library mode study notes of the PHP design mode. I hope this article will help you as follows.

Script ec (2); script

1. mode 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

Namespace DesignPatterns \ More \ Repository;

/**
* Post class
* @ 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

Namespace DesignPatterns \ More \ Repository;

Use DesignPatterns \ More \ Repository \ Storage;

/**
* Repository corresponding to Post
* This class is between the data entity layer (Post) and the Access Object layer (Storage ).
*
* Repository encapsulates persistent objects to data storage and displays object-oriented view operations on the display layer.
*
* Repository also implements the separation and unidirectional dependency between the domain layer and the data ing layer.
*
* PostRepository class
* @ Package DesignPatterns \ Repository
*/
Class PostRepository
{
Private $ persistence;

Public function _ construct (Storage $ persistence)
{
$ This-> 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 a specified Post object
*
* @ Param Post $ post
* @ Return bool
*/
Public function delete (Post $ post)
{
Return $ this-> persistence-> delete ($ post-> getId ());
}
}
Storage. php

Namespace DesignPatterns \ More \ Repository;

/**
* Storage Interface
*
* This interface defines how to access the data storage.
* The specific implementation can be diversified, such as memory, relational databases, NoSQL databases, and so on.
*
* @ Package DesignPatterns \ Repository
*/
Interface Storage
{
/**
* Persistent data method
* Returns the ID of the newly created object.
*
* @ Param array () $ data
* @ Return int
*/
Public function persist ($ data );

/**
* Return data by specifying the id
* If it is null, null is returned.
*
* @ Param int $ id
* @ Return array | null
*/
Public function retrieve ($ id );

/**
* Delete data by specifying an id
* If the data does not exist, false is returned; otherwise, true is returned if the deletion is successful.
*
* @ Param int $ id
* @ Return bool
*/
Public function delete ($ id );
}

MemoryStorage. php

Namespace DesignPatterns \ More \ Repository;

Use DesignPatterns \ More \ Repository \ Storage;

/**
* MemoryStorage class
* @ 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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.