forked from 734380794/design-patterns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15-外观模式.php
80 lines (68 loc) · 1.21 KB
/
15-外观模式.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
<?php
declare(strict_types=1);
/*
* This file is modified from `xiaohuangniu/26`.
*
* @see https://github.com/xiaohuangniu/26
*/
header('Content-type: text/html; charset=utf-8');
/**
* 动物接口.
*/
interface AnimalInterface
{
public function Produce(); // 生产方法
}
/**
* 创建 - 鸡模型.
*/
class ChiCken implements AnimalInterface
{
public function Produce()
{
echo '这是一只鸡~'.PHP_EOL;
}
}
/**
* 创建 - 猪模型.
*/
class Pig implements AnimalInterface
{
public function Produce()
{
echo '这是一只猪~'.PHP_EOL;
}
}
/**
* 外观类.
*/
class AnimalMaker
{
private $_chicken; // 鸡模型实例
private $_pig; // 猪模型实例
public function __construct()
{
$this->_chicken = new Chicken();
$this->_pig = new Pig();
}
/**
* 生产鸡
*/
public function produceChicken()
{
$this->_chicken->produce();
}
/**
* 生产猪.
*/
public function producePig()
{
$this->_pig->produce();
}
}
// 初始化外观类
$animalMaker = new AnimalMaker();
// 生产一只猪
$animalMaker->producePig();
// 生产一只鸡
$animalMaker->produceChicken();