-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathsimpleFactory.php
53 lines (45 loc) · 1.14 KB
/
simpleFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
/**
* Created by PhpStorm.
* User: lock
* Date: 2017/7/17
* Time: 11:53
* 简单工厂模式
* 简单工厂的作用是实例化对象,而不需要客户了解这个对象属于哪个具体的子类。
* 简单工厂实例化的类具有相同的接口或者基类,在子类比较固定并不需要扩展时,可以使用简单工厂。
*/
class dbFactory{
protected $typeList = [];
public function __construct() {
$this->typeList = array(
'mysql'=>'mysql',
'oracle'=>'oracle'
);
}
public function getDb($type){
if(!in_array($type,$this->typeList)){
exit('error');
}
$className = $this->typeList[$type];
return new $className;
}
}
/**
* Interface connect
*/
interface connect{
public function connect_db();
}
class mysql implements connect{
public function connect_db() {
echo 'mysql connect to db server ...';
}
}
class oracle implements connect{
public function connect_db() {
echo 'oracle connect to db server ...';
}
}
$dbObj = new dbFactory();
$mysql = $dbObj->getDb('mysql');
$mysql->connect_db();