factory reset php design mode Factory factory mode

WBOY
Release: 2016-07-29 08:45:46
Original
1129 people have browsed it

复制代码 代码如下:


/**
* Factory Method Pattern
*
* Define an interface for creating objects, let subclasses decide which class to instantiate, and use one class to defer instantiation 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
";
}
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");

以上就介绍了factory reset php设计模式 Factory工厂模式,包括了factory reset方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!