-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactoryMethod.php
More file actions
49 lines (41 loc) · 843 Bytes
/
Copy pathFactoryMethod.php
File metadata and controls
49 lines (41 loc) · 843 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
<?php
/**
* 工厂方法模式
*/
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;
}
}
abstract class MsgFactory {
abstract public function factoryMethod();
public function getMsg() {
return $this->factoryMethod();
}
}
class AliyunFactory extends MsgFactory {
public function factoryMethod()
{
return new AliyunMsg();
}
}
class CtyFactory extends MsgFactory {
public function factoryMethod()
{
return new CtyMsg();
}
}
$ctyFactory = new CtyFactory();
$ctyFactory->factoryMethod()->send();
$msg = $ctyFactory->getMsg();
$msg->send();