Notice

YouTube.com/BESTTravelers - Visit and SUBSCRIBE to our travel related YouTube Channel named "BEST Travelers"

Sunday, July 31, 2011

Registry Design Pattern example using PHP

It's a bhavioral Pattern – It’s generally considered “good form” to avoid the use of global variables, objects are usually passed from one code segment to another as parameters.

The problem with passing instances globally is that objects sometimes end up as “tramp data,” passed into one function only to be passed again to another function which truly needs the object. To make writing, reading, and consuming code simpler, it’s best to minimize the number of different objects and consolidate knowledge of how to get to a numerous of other widely-used objects into a single, well-known object.

How can you get references to objects through a single, well-known, object? The Registry design pattern is like an “object phone book”—a directory—that stores and retrieves references to objects.

The Registry pattern can be useful, for example, if, for the bulk of your application, you use the same database connection, but need to connect to an alternate database to perform a small set of tasks every now and then. If your DB class is implemented as a Singleton, this is impossible (unless you implement two separate classes, that is)—but a Registry makes it very easy:

<?php

class Profile {    
  protected $_address1;
  protected $_address2;
  protected $_city;
  protected $_state;
  protected $_zip;
  protected $_phone;
  protected $_fax;
   
  public function __construct() {
       
  }
   
  public function address1($address = NULL) {
    if(isset($address)) {
      if(!is_string($address)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_address1 = $address;
    } else {
      return $this->_address1;
    }       
  }
   
  public function address2($address = NULL) {
    if(isset($address)) {
      if(!is_string($address)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_address2 = $address;
    } else {
      return $this->_address2;
    }
  }
   
  public function city($city = NULL) {
    if(isset($city)) {
      if(!is_string($city)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_city = $city;
    } else {
      return $this->_city;
    }
  }
   
  public function state($state = NULL) {
    if(isset($state)) {
      if(!is_string($state)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_state = $state;
    } else {
      return $this->_state;
    }
  }
   
  public function zip($zip = NULL) {
    if(isset($zip)) {
      if(!is_string($zip)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_zip = $zip;
    } else {
      return $this->_zip;
    }
  }
   
  public function phone($phone = NULL) {
    if(isset($phone)) {
      if(!is_string($phone)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_phone = $phone;
    } else {
      return $this->_phone;
    }
  }    
  public function fax($fax = NULL) {
    if(isset($fax)) {
      if(!is_string($fax)) throw new ProfileEx(__METHOD__.' expects a string parameter.');
      $this->_fax = $fax;
    } else {
      return $this->_fax;
    }
  }
   
}
?> 
Source: From lecture notes of Design and Development Open Multi-tier Application 

No comments:

Post a Comment