-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path13-桥接模式.php
85 lines (73 loc) · 1.35 KB
/
13-桥接模式.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
<?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');
/**
* 抽象 道路.
*/
abstract class Road
{
public $ICAR; // 汽车的实例
abstract public function RoRun();
}
/**
* 创建 - 高速公路.
*/
class SpeedRoad extends Road
{
public function RoRun()
{
$this->ICAR->IcRun();
echo ':极速的行驶在高速公路上!'.PHP_EOL;
}
}
/**
* 创建 - 乡村小道.
*/
class StreetRoad extends Road
{
public function RoRun()
{
$this->ICAR->IcRun();
echo ':欢快的行驶在乡村小道上!'.PHP_EOL;
}
}
/**
* 抽象接口 车.
*/
interface ICAR
{
public function IcRun();
}
/**
* 创建 - 轿车.
*/
class Car implements ICAR
{
public function IcRun()
{
echo '轿车';
}
}
/**
* 创建 - 马车.
*/
class Carriage implements ICAR
{
public function IcRun()
{
echo '马车';
}
}
// 让轿车跑到 乡村小道
$speedRoad = new StreetRoad(); // 先创建乡村小道
$speedRoad->ICAR = new Car(); // 再把轿车放到马路上
$speedRoad->RoRun();
// 让马车跑到 高速公路
$street = new SpeedRoad(); // 先创建高速公路
$street->ICAR = new Carriage(); // 再把马车放到马路上
$street->RoRun();