Tuesday, February 06, 2007

 
DESIGN PATTERNS:

Starting this artcile I would cover different design patterns focusing PHP. But before I proceed, for those who are new to this, let me introduce you briefly to "Design Patterns".

According to Wikipedia, the free encyclopedia:

"A design pattern is a general repeatable solution to a commonly occurring problem in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that can be used in many different situations. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved."

Creating flexible, extensible and portable applications is largely dependent upon how well we decouple, a system from the process involved with object composition, creation and representation. Most of the time, it is possible to abstract these processes by creating patterns, which in turn should take some of the task and hence relasing the burden of object construction and class referencing.

Sometimes a single instance of a class is required to effectively manage a task. For example in database operation we need only one instance of object to be present in memory. In such cases Singleton pattern is ideal.


The Singleton pattern possesses two general features:

* The default class constructor cannot be called. Instead, it will be called through a designated class maintainer method that ensures only one instantiation is available at any given time.

* A static class member that holds a reference to the existing class resource identifier.



For example consider fraction of code from DB:

class cMySql
{
/**
* This should hold the value of DB Name
*
* @var string
*/
private $strDBName;

/**
* This should hold the username details for the DB
*
* @var string
*/
private $strUserName;

/**
* This holds the database password
*
* @var string
*/
private $strPWD;

/**
* This preserves the handle
*
* @var resource
*/
private $cHandle;

/**
* This holds the error messages generated during the execution
*
* @var string
*/
private $strStatus;

/**
* This attribute will make sure that only one resource is available at one time
*
* @var cMySql
*/

private static $stExists = NULL;


/**
* This method would intiate a db connection
* @param string $host
* @param string $User
* @param string $Pwd
* @param string $dbName
*/

static function Initialize($host, $usr, $pwd, $dbname)
{
$this->strHost = $host;
$this->strUserName = $usr;
$this->strPWD = $pwd;
$this->strDBName = $dbname;

if(self::$stExists === NULL)
{
self::$stExists = new cMySql();
}
return self::$stExists;
}

public function Connect()
{
$this->cHandle = mysql_connect($this->strHost, $this->UserName, $this->strPWD);
if($this->cHandle)
{
$cDB = mysql_select_db($this->strDBName, $cHandle);
}
else
{
$this->strStatus = 'Encountered an error while connecting to the host '.mysql_error();
}
}

....
....
}

Now this class functionality will make sure that at any given point of time, only one object will be initiated.



example
$objDB = '';

$objDB = cMySql::Initialize($this->strHost, $this->UserName, $this->Pwd, $this->DBName);
$objDB->Connect();
....
....
and so on...

Comments: Post a Comment



<< Home

This page is powered by Blogger. Isn't yours?