PHP design mode Factory (Factory mode) _php Tutorial

Source: Internet
Author: User
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 ...

  • Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.