Copy CodeThe code is 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 () {
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.