forked from uploo/python3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractFactory.php
More file actions
124 lines (95 loc) · 2.07 KB
/
AbstractFactory.php
File metadata and controls
124 lines (95 loc) · 2.07 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
<?php
//生产引擎的标准
interface engineNorms{
function engine();
}
class carEngine implements engineNorms{
public function engine(){
return '汽车引擎';
}
}
class busEngine implements engineNorms{
public function engine(){
return '巴士车引擎';
}
}
//生产车身的标准
interface bodyNorms{
function body();
}
class carBody implements bodyNorms{
public function body(){
return '汽车车身';
}
}
class busBody implements bodyNorms{
public function body(){
return '巴士车车身';
}
}
//生产车轮的标准
interface whellNorms{
function whell();
}
class carWhell implements whellNorms{
public function whell(){
return '汽车轮子';
}
}
class busWhell implements whellNorms{
public function whell(){
return '巴士车轮子';
}
}
//工厂标准
interface factory{
static public function getInstance($type);
}
//汽车工厂
class carFactory implements factory{
static public function getInstance($type){
$instance='';
switch($type){
case 'engine':
$instance=new carEngine();
break;
case 'body':
$instance=new carBody();
break;
case 'whell':
$instance=new carWhell();
break;
default:
throw new Exception('汽车工厂无法生产这种产品');
}
return $instance;
}
}
//巴士车工厂
class busFactory implements factory{
static public function getInstance($type){
$instance='';
switch($type){
case 'engine':
$instance=new busEngine();
break;
case 'body':
$instance=new busBody();
break;
case 'whell':
$instance=new busWhell();
break;
default:
throw new Exception('巴士车工厂无法生产这种产品');
}
return $instance;
}
}
$car['engine']=carFactory::getInstance('engine')->engine();
$car['body']=carFactory::getInstance('body')->body();
$car['whell']=carFactory::getInstance('whell')->whell();
print_r($car);
$bus['engine']=busFactory::getInstance('engine')->engine();
$bus['body']=busFactory::getInstance('body')->body();
$bus['whell']=busFactory::getInstance('whell')->whell();
print_r($bus);