-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory.php
More file actions
94 lines (80 loc) · 1.65 KB
/
Copy pathFactory.php
File metadata and controls
94 lines (80 loc) · 1.65 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
<?php
/**
* 简单工厂模式
*/
class Factory {
public static function createProduct($type)
{
$product = null;
switch ($type) {
case 'A':
$product = new ProductA();
break;
case 'B':
$product = new ProductB();
break;
}
return $product;
}
}
interface Product {
public function show();
}
class ProductA implements Product {
public function show()
{
echo '展示A...' . PHP_EOL;
}
}
class ProductB implements Product {
public function show()
{
echo '展示B...' . PHP_EOL;
}
}
$productA = Factory::createProduct('A');
$productB = Factory::createProduct('B');
$productA->show();
$productB->show();
/*
* 实例
*/
class MsgFactory {
public static function createFactory($type) {
switch ($type) {
case 'aliyun':
return new AliyunMsg();
case 'cty':
return new CtyMsg();
case 'jg':
return new JdMsg();
default:
return null;
}
}
}
interface Msg {
public function send();
}
class AliyunMsg implements Msg {
public function send()
{
echo '发送阿里云短信...' . PHP_EOL;
}
}
class CtyMsg implements Msg {
public function send()
{
echo '发送畅天游短信...' . PHP_EOL;
}
}
class JdMsg implements Msg {
public function send()
{
echo '发送京东短信...' . PHP_EOL;
}
}
$aliyunMsg = MsgFactory::createFactory('aliyun');
$ctyMsg = MsgFactory::createFactory('cty');
$aliyunMsg->send();
$ctyMsg->send();