Friday, September 3, 2010

Design Patterns: Singleton

What is Singleton ?
i am not a good person to define things, but any way , when you feel you need only one instance from a class then you need a singleton!
Did computer scientists waste their time to instantiate only one object?
absolutely no , it is a very useful solution , some time having multiple copies of objects can lead to chaos , resource looking , resource over used , …. etc
When it is needed?
Most of the common usage is to handle global setting like database connections , registry settings  , who is online now ? , .etc

here is an example to implement singleton while connecting to MYSQL connection

<?php
//ConnectionConf.php
class ConnectionConf 
{
public $Username; 
public $Password; 
public $ServerAddress; 
public $Database;
}
?>
<?php
require_once 'ConnectionConf.php';
class Connection
{
/**
* Varaible to hold the unique instance  
* @var Connection
*/
private static $UniqueInstance; 
/**
* Connetion User name 
* @var string
*/
private $_strUsername; 
/**
* Connection Password
* @var string
*/
private $_strPassword; 
/**
* Connection Address
* @var string
*/
private $_strServerAddress; 
/**
* Database Name 
* @var string
*/
private $_strDatabase; 
/**
* Last Error happend 
* @var string
*/
private $_strLastError; 
/**
* mysql link identfier 
* @var mysql resource
*/
private $_resConnectionResource; 
/**
* Class constructor , it is private , 
* so no way you can create it using new Connection 
* @param $ConnectionConf ConnectionConf
* @return void
*/
private function __construct($ConnectionConf)
{
$this->_strUsername=$ConnectionConf->Username; 
$this->_strPassword=$ConnectionConf->Password; 
$this->_strServerAddress=$ConnectionConf->ServerAddress; 
$this->_strDatabase=$ConnectionConf->Database;
$this->StartConnection(); 
}
/**
* @param $ConnectionConf ConnectionConf
* @return Connection
*/
public static function GetInstance ($ConnectionConf=null) 
{
if (Connection::$UniqueInstance==null)
{
if ($ConnectionConf==null)
throw new Exception("Connection Configuration Can't be null at first time"); 
Connection::$UniqueInstance = new Connection($ConnectionConf); 
return Connection::$UniqueInstance ; 
}
return Connection::$UniqueInstance ; 
}
/////////////////////////////// Sets and Gets Start/////////////////////////////////// 
public function GetUsername () 
{
return $this->_strUsername;
}
public function GetPassword () 
{
return $this->_strPassword; 
}
public function GetServerAddress () 
{
return $this->_strServerAddress; 
}
public function GetDatabaseName()
{
return $this->_strDatabase;  
}
public function GetConnectionResource () 
{
return $this->_resConnectionResource; 
}
public function GetLastError () 
{
return $this->_strLastError;
}
////////////////////////////Sets And Gets End/////////////////////////////////////////////////////
/**
* 
* @return bool
*/
public function StartConnection () 
{
try{
$this->_resConnectionResource=mysql_connect($this->_strServerAddress,$this->_strUsername,

$this >_strPassword);
mysql_select_db($this->_strDatabase);
}
catch(Exception $e){
$this->_strLastError=$e->getMessage(); 
return 0 ;   
}
return 1; 
}
public function CloseConnection () 
{
try{
mysql_close($this->_resConnectionResource); 
}
catch (Exception $e)
{
$this->_strLastError=$e->getMessage(); 
return 0 ; 
}
return 1; 
}
public function RestartConnection () 
{
$this->CloseConnection(); 
$this->StartConnection(); 
}
}
$Config = new ConnectionConf(); 
$Config->Username= "root"; 
$Config->Password= ""; 
$Config->ServerAddress= "localhost"; 
$Config->Database= "road";
//First instance 
$Connection = Connection::GetInstance($Config);
var_dump($Connection); 
//will return the same instance declared before
$Connection1 = Connection::GetInstance(); 
var_dump($Connection1);
?>



here are the files


http://www.4shared.com/file/9d_RHQ0I/ConnectionConf.html
http://www.4shared.com/file/DoOt0KUh/singleton.html

No comments: