PHP design Pattern Repository Resource Library Model learning notes

Source: Internet
Author: User
Tags datetime gettext to domain

1. Pattern definition

Repository is a separate layer between the domain layer and the Data map layer (the data access layer). Its presence makes the domain layer less aware of the existence of the data access layer, providing a similar set of interfaces that provide domain-level access to domain objects. Repository is the warehouse administrator, the domain layer needs to tell the warehouse administrator, the warehouse manager to the things to it, do not need to know where things actually put.

The Repository pattern is an architectural pattern, which is useful when designing the architecture. The benefits of applying the Repository pattern are much higher than the code added to implement this pattern. This pattern should be used as long as the project is layered.

2, UML class diagram

3. Sample Code

post.php

<?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

<?php

namespace Designpatterns\more\repository;

Use Designpatterns\more\repository\storage;

/**
* Post corresponds to the Repository
* The 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 at the presentation layer
*
* Repository also implements the separation and one-way dependency of the domain and data mapping layers
*
* Postrepository Class
* @package Designpatterns\repository
*/
Class Postrepository
{
Private $persistence;

Public function __construct (Storage $persistence)
{
$this->persistence = $persistence;
}

   /**
     * Returns the Post object by specifying an 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
     */
&NBSP;&N bsp;  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

<?php

namespace Designpatterns\more\repository;

/**
* Storage Interface
*
* This interface defines a way to access the data memory
* Specific implementations can be diverse, such as memory, relational databases, NoSQL databases, etc.
*
* @package Designpatterns\repository
*/
Interface Storage
{
/**
* Persistent Data method
* Returns the newly created object ID
*
* @param array () $data
* @return int
*/
Public function persist ($DATA);

/**
* Return data by specifying ID
* If NULL is returned for NULL
*
* @param int $id
* @return Array|null
*/
Public function retrieve ($id);

/**
* Delete data by specifying ID
* If the data does not exist returns false if the deletion succeeds returns true
*
* @param int $id
* @return BOOL
*/
Public Function Delete ($id);
}

memorystorage.php

<?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.