Copy CodeThe code is as follows:
/**
* Factory Method mode
*
* Define an interface for creating objects, let subclasses decide which class to instantiate, defer to its subclasses with instantiation of a class
*/
/*
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
";
}
Public Function connect () {
}
Public function exec () {
}
}
Class Postgredb implements DB
{
Public Function __construct () {
echo "Postgre db
";
}
Public Function connect () {
}
Public function exec () {
}
}
Class Mssqldb implements DB
{
Public Function __construct () {
echo "MSSQL DB
";
}
Public Function connect () {
}
Public function exec () {
}
}
$oMysql = Dbfactory::create ("Mysql");
$oPostgre = Dbfactory::create ("Postgre");
$oMssql = Dbfactory::create ("Mssql");
http://www.bkjia.com/PHPjc/323794.html www.bkjia.com true http://www.bkjia.com/PHPjc/323794.html techarticle Copy the code code as follows:? PHP/** * Factory Method Mode * * Defines an interface for creating objects, letting subclasses decide which class to instantiate, using instantiation of a class to defer to its child ...