forked from wenjy/design_patten_php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbstractFactory.php
76 lines (62 loc) · 1.44 KB
/
AbstractFactory.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
/**
* 抽象工厂模式
*
* 提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类
*
*/
interface IDepartment
{
public function insert(Department $department);
public function getDepartment($id);
}
class MySqlDepartment implements IDepartment
{
public function insert(Department $department)
{
echo 'mysql 新增记录' . PHP_EOL;
}
public function getDepartment($id)
{
echo 'mysql 查询一条记录' . PHP_EOL;
}
}
class SqlServerDepartment implements IDepartment
{
public function insert(Department $department)
{
echo 'sqlServer 新增记录' . PHP_EOL;
}
public function getDepartment($id)
{
echo 'sqlServer 查询一条记录' . PHP_EOL;
}
}
interface InterfaceFactory
{
public function createDepartment();
}
class SqlServerFactory implements InterfaceFactory
{
public function createDepartment()
{
return new SqlServerDepartment();
}
}
class MySqlFactory implements InterfaceFactory
{
public function createDepartment()
{
return new MySqlDepartment();
}
}
class Department
{
}
$department = new Department();
$iDepartment = (new MySqlFactory())->createDepartment();
$iDepartment->insert($department);
$iDepartment->getDepartment(1);
$iDepartment = (new SqlServerFactory())->createDepartment();
$iDepartment->insert($department);
$iDepartment->getDepartment(1);