Php design mode Factory (Factory mode)

Source: Internet
Author: User
Defines an interface for creating objects, so that the subclass determines which class to instantiate and delays the instantiation of a class to its subclass. Copy codeThe code is as follows:
/**
* Factory method mode
*
* Defines an interface for creating objects so that the subclass decides which class to instantiate and delays the instantiation of a class to its subclass.
*/

/*
Class DBFactory
{
Public static function create ($ type)
{
Swtich ($ type)
{
Case "Mysql ":
Return new MysqlDB (); break;
Case "Postgre ":
Return new Analytic DB (); 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 Analytic DB 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 ");

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.