forked from hulin32/design-patterns-by-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFactory.php
More file actions
193 lines (161 loc) · 3.58 KB
/
AbstractFactory.php
File metadata and controls
193 lines (161 loc) · 3.58 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/////////version1
//数据库访问
class User
{
private $id = null;
public function setId($id)
{
$this->id = $id;
}
public function getId($id)
{
return $this->id;
}
private $name = null;
public function setName($name)
{
$this->name = $name;
}
public function getName($name)
{
return $this->id;
}
}
class Department
{
private $id = null;
public function setId($id)
{
$this->id = $id;
}
public function getId($id)
{
return $this->id;
}
private $name = null;
public function setName($name)
{
$this->name = $name;
}
public function getName($name)
{
return $this->id;
}
}
interface IUser
{
public function insert(User $user);
public function getUser($id);
}
class SqlserverUser implements IUser
{
public function insert(User $user)
{
echo "往SQL Server中的User表添加一条记录\n";
}
public function getUser($id)
{
echo "根据id得到SQL Server中User表一条记录\n";
}
}
class AcessUser implements IUser
{
public function insert(User $user)
{
echo "往Acess Server中的User表添加一条记录\n";
}
public function getUser($id)
{
echo "根据id得到Acess Server中User表一条记录\n";
}
}
// interface IFactory
// {
// public function CreateUser();
// public function CreateDepartment();
// }
// class SqlserverFactory implements IFactory
// {
// public function CreateUser()
// {
// return new SqlserverUser();
// }
// public function CreateDepartment()
// {
// return new SqlserverDepartment();
// }
// }
// class AcessFactory implements IFactory
// {
// public function CreateUser()
// {
// return new AcessUser();
// }
// public function CreateDepartment()
// {
// return new AcessDepartment();
// }
// }
//简单工厂替换抽象工厂
class DataBase
{
const DB = 'Sqlserver';
// private $db = 'Access';
public static function CreateUser()
{
$class = static::DB.'User';
return new $class();
}
public static function CreateDepartment()
{
$class = static::DB.'Department';
return new $class();
}
}
interface IDepartment
{
public function insert(Department $user);
public function getDepartment($id);
}
class SqlserverDepartment implements IDepartment
{
public function insert(Department $department)
{
echo "往SQL Server中的Department表添加一条记录\n";
}
public function getDepartment($id)
{
echo "根据id得到SQL Server中Department表一条记录\n";
}
}
class AcessDepartment implements IDepartment
{
public function insert(Department $department)
{
echo "往Acess Server中的Department表添加一条记录\n";
}
public function getDepartment($id)
{
echo "根据id得到Acess Server中Department表一条记录\n";
}
}
//客户端代码
// $user = new User();
// $iu = (new AcessFactory())->CreateUser();
// $iu->insert($user);
// $iu->getUser(1);
// $department = new Department();
// $id = (new AcessFactory())->CreateDepartment();
// $id->insert($department);
// $id->getDepartment(1);
/////////////////////////////////////////////////
//改为简单工厂后的客户端代码
$user = new User();
$iu = DataBase::CreateUser();
$iu->insert($user);
$iu->getUser(1);
$department = new Department();
$id = DataBase::CreateDepartment();
$id->insert($department);
$id->getDepartment(1);