-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfactory_method.php
More file actions
55 lines (44 loc) · 940 Bytes
/
factory_method.php
File metadata and controls
55 lines (44 loc) · 940 Bytes
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
<?php
/*
* 工厂方法模式
*/
interface people {
function jiehun();
}
class man implements people {
function jiehun() {
echo '送玫瑰,送戒指!<br>';
}
}
class women implements people {
function jiehun() {
echo '穿婚纱!<br>';
}
}
// 注意了,这里是简单工厂本质区别所在,将对象的创建抽象成一个接口。
interface createMan {
function create();
}
class FactoryMan implements createMan {
function create() {
return new man;
}
}
class FactoryWomen implements createMan {
function create() {
return new women;
}
}
class Client {
// 简单工厂里的静态方法
function test() {
$Factory = new FactoryMan;
$man = $Factory->create();
$man->jiehun();
$Factory = new FactoryWomen;
$man = $Factory->create();
$man->jiehun();
}
}
$f = new Client;
$f->test();