Copy Code code as follows:
<?php
/**
* Factory Method mode
*
* Define an interface for creating objects, let subclasses decide which class to instantiate, and delay the instantiation of a class to its subclasses
*/
/*
Class Dbfactory
{
public static function Create ($type)
{
Swtich ($type)
{
Case "Mysql":
return new MySQLdb (); Break
Case "Postgre":
return new Postgredb (); Break
Case "Mssql":
return new Mssqldb (); Break
}
}
}
*/
Class Dbfactory
{
public static function Create ($type)
{
$class = $type. " DB ";
return new $class;
}
}
Interface DB
{
Public function connect ();
Public function exec ();
}
Class MySQLdb implements DB
{
Public Function __construct () {
echo "MySQL db<br/>";
}
Public Function connect () {
}
Public function exec () {
}
}
Class Postgredb implements DB
{
Public Function __construct () {
echo "Postgre db<br/>";
}
Public Function connect () {
}
Public function exec () {
}
}
Class Mssqldb implements DB
{
Public Function __construct () {
echo "MSSQL db<br/>";
}
Public Function connect () {
}
Public function exec () {
}
}
$oMysql = Dbfactory::create ("Mysql");
$oPostgre = Dbfactory::create ("Postgre");
$oMssql = Dbfactory::create ("Mssql");