-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstractFactory.php
186 lines (148 loc) · 3.02 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
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
<?php
/*
抽象工厂:围绕一个超级工厂创建其他工厂。该超级工厂又称为其他工厂的工厂
注意:这里和工厂方法的区别是:一系列,而工厂方法则是一个。
那么,我们是否就可以想到在接口create里再增加创建“一系列”对象的方法呢?
*/
interface people
{
public function givGift();
}
class YoungMan implements people
{
public function givGift()
{
echo "暗恋";
}
}
class OldMan implements people
{
public function givGift()
{
echo "送钻戒";
}
}
class YoungWoman implements people
{
public function givGift()
{
echo "害羞";
}
}
class OldWoman implements people
{
public function givGift()
{
echo "接受";
}
}
interface AbstractFactory
{
public function createOpen();
public function createOld();
}
class ManFactory implements AbstractFactory
{
public function createOpen()
{
return new OldMan();
}
public function createOld()
{
return new YoungMan();
}
}
class WomanFactory implements AbstractFactory
{
public function createOpen()
{
return new OldWoman();
}
public function createOld()
{
return new YoungWoman();
}
}
class Client
{
public static function main()
{
self::run(new ManFactory());
self::run(new WomanFactory());
}
public static function run(AbstractFactory $factory)
{
$res = $factory->createOld();
$res->giveGift();
$factory->createOpen();
$res->giveGift();
}
}
$client = new Client();
$client->main();
###############################################
//抽象工厂
interface AnimalFactory {
public function createCat();
public function createDog();
}
//具体工厂
class BlackAnimalFactory implements AnimalFactory {
function createCat(){
return new BlackCat();
}
function createDog(){
return new BlackDog();
}
}
class WhiteAnimalFactory implements AnimalFactory {
function createCat(){
return new WhiteCat();
}
function createDog(){
return new WhiteDog();
}
}
//抽象产品
interface Cat {
function Voice();
}
interface Dog {
function Voice();
}
//具体产品
class BlackCat implements Cat {
function Voice(){
echo '黑猫喵喵……';
}
}
class WhiteCat implements Cat {
function Voice(){
echo '白猫喵喵……';
}
}
class BlackDog implements Dog {
function Voice(){
echo '黑狗汪汪……';
}
}
class WhiteDog implements Dog {
function Voice(){
echo '白狗汪汪……';
}
}
//客户端
class Client {
public static function main() {
self::run(new BlackAnimalFactory());
self::run(new WhiteAnimalFactory());
}
public static function run(AnimalFactory $AnimalFactory){
$cat = $AnimalFactory->createCat();
$cat->Voice();
$dog = $AnimalFactory->createDog();
$dog->Voice();
}
}
Client::main();
?>