Anda di halaman 1dari 5

1

------------------
Quick review
at Design
patterns
------------------
Quick review at Design patterns

Design patterns are the solutions to day to day programming problems.


PHP5 has good support for object oriented programming approach.
Design patterns are standard class designing techniques.
There are lots of design patterns but only few are accepted and become standard design patterns.

Here are some standard design patterns.


1. Adapter pattern.
2. Factory pattern.
3. Singleton pattern.
4. Active Record.
5. Row data gateway pattern.
6. Registry pattern.
7. MVC

1. Adapter pattern

Adapter class is nothing but a simple class which adapts the methods of base class and simply uses
them to carry out new functionality.

When to use this pattern?

I have some class libraries which I need in almost all projects to achieve common tasks like
authentication, database connections etc. I normally include all those classes and the rest
application script makes use of those classes by instantiating class objects. This technique looks
fine in any case. But at some instance I need to have some more functionally to be carried out by
one of old the classes And I can’t change the class files directly. This issue can be solved by
adapter design pattern. Adapter class is nothing but a simple class which adapts the methods of
base class and simply uses them to carry out new functionality.

How to identify the adapter pattern?

When constructor of a class takes object of another class and one of the method of new class
Adapt the methods of the object passed in constructor then it is an adapter class.

class employee class employeeAdapter


{ {
private $first_name,$last_name; private $employee;

function __construct($fname,$lname) function __construct(employee $employee)


{ {
$this->first_name = $fname; $this->employee = $employee;
$this->last_name = $lname; }
} public function getFullname()
public function getFirstName() {
{ return $this->employee-
return $this->first_name; >getFirstName()." - ".$this->employee-
} >getLastName();
public function getLastName() }
{ }
return $this->last_name;
}
}
$obj = new employee('firstname','lastname');
$objAdapter = new employeeAdapter($obj);
echo $objAdapter->getFullname();

2. Factory pattern

I have a generic class to connect to database server and select the database. I normally use this
class to handle my database connections. But I can have different database servers in different
projects like MySQL, Microsoft SQL server, oracle etc.... In this case I can have a factory method
in my generic class which gives facility to create an instance of one or more other classes (In my
case these classes are classes for different databases.)

How to identify
When there is any static function in class which instantiate other objects depend on some condition
then it can be a factory method implementation.

What’s this static method?


Declaring class members or methods as static, makes them callable from outside the object
context. Also we can’t call the static member or method by class object.
class mysqlClass class Database
{ {
function __construct($config = // This is a factory method because it
array()) allows us to instantiate other class
{ public static function
echo 'connecting to mysql dbFactory($db,$config = array())
server ..'; {
switch($db)
} {
} case "mysql" :
$dbObject = new mysqlClass();
class mssqlClass
{ case "mssql" :
function __construct($config = $dbObject = new mssqlClass();
array()) }
{ return $dbObject;
echo 'connecting to mssql }
server ..'; }
}
}

$db = Database::dbFactory('mysql',array('host','username','pass','dbname'));

3. Singleton pattern

This is the simplest design pattern. This pattern is used in application when we don’t want the
duplication of access to some resource. In simple words only one instance of class can be created,
and only one instance is used in whole application. Mostly this kind of implementation is used for
database connections, to have only one database connection at a time.

class mysqlClass
{
protected static $_instance;

public $username;

function __construct()
{
}

public static function getInstance()


{
if(null === self::$_instance)
{
self::$_instance = new mysqlClass();
}
return self::$_instance;
}
}
$singleDbConnection = mysqlClass::getInstance();
$singleDbConnection->username = 'user1';

$isItOtherDbConnection = mysqlClass::getInstance();
print $isItOtherDbConnection->username;
// c the output of above line it prints the value which is set by the first
object. Now this is what is singleton.
Dhanesh Mane
http://www.dhaneshmane.com
http://twitter.com/dhaneshmane

Anda mungkin juga menyukai