Registry Design Pattern

Setup

The following files are needed for this exercise..

Motivation

Although the MVC Pattern makes an application more flexible and easier to test, sometimes data or objects contained in one layer needs to be accessed in another layer. The data could be passed from object to object through each layer but this causes loose coupling.

The Registry Pattern is a single class that provides data to all objects within the application through static or instance methods.

UML Diagram

Registry Class

Usage

The Registry Class can be used to retrieve the HTTP request and HTTP response in an application.

    $registry = Registry::GetInstance();
    $response = $registry->GetResponse();

Implementation

As we have seen, when a user requests a web page or posts form data, A HTTP request is sent to the server which returns a HTTP response. These are 2 objects that we will need to access through most layers within the application. The request and response classes are as follows:

Request.php

namespace base;


class Request
{
    private $_request;

    function __construct()
    {
        $this->Init();
    }

    private function Init()
    {
        if(isset($_SERVER['REQUEST_METHOD']))
        {
            $this->_request = $_REQUEST;
        } else
        {
            $this->_request = array();
        }
    }

    public function GetRequest($key)
    {
        if(isset($this->_request[$key]))
        {
            return $this->_request[$key];
        }
        else
        {
            return;
        }
    }

    public function SetRequest($key, $value)
    {
        $this->_request[$key]  = $value;
    }
}

Looking at the request class, in its constructor the Init() method is invoked. In this method, if the server request (This can be a GET or POST) is set, it sets it to the request attribute. If the server request is not set, sets the request to an empty array.

The property methods (GET and SET) are also used, allowing us to retrieve the request by using the GetRequest() method and testing the request class through SetRequest().

Response.php

namespace base;
    class Response
{
    //attributes
    private $_response = array();

    //constructor
    public function __construct()
    {

    }

    //properties

    public function GetResponse($key)
    {
       if (isset($this->_response[$key]))
       {
           return $this->_response[$key];
       }

       return;
    }

    public function SetResponse($key, $value)
    {
        $this->_response[$key] = $value;
    }

    public function GetView()
    {
        if (isset($this->_response['view']))
        {
            return $this->_response['view'];
        }
        return false;
    }

    public function SetView($view)
    {
        if(is_string($view))
        {
            $this->_response['view'] = $view;
        }
    }

The response class stores the response in an array. Likewise to the Request class, the Response class has getter and setter methods. Using the get method, the array key is passed in to retrieve the appropriate response. When setting the response, the array key and the response value is passed in. Additional to the Request class, the response class contains methods to get the View and set The view (We will look closer at these methods later on).

Excercise

You must complete the Registry Class to contain both Request and Response Objects, allowing the Registry class to provide access to both. Once completed Add a database connection to the Registry Class


    namespace base;
use \base\Response;
use \base\Request;

require_once 'Response.php';
require_once 'Request.php';


class Registry
{
    private static $_instance;
    private $_request;
    private $_response;

    private $_db; // this is the database connection

    private function __construct()
    {

    }

    static function GetInstance()
    {

    }

    //Get Request

    //Set Request

    //Get Response

    //Set Response
}

Hints

Hint 1 (Click To View)

Hint 2 (click to View)