forked from eimg/design-patterns-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11-bridge.php
55 lines (45 loc) · 809 Bytes
/
11-bridge.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
<?php
interface Engine
{
public function run();
}
class Diesel implements Engine
{
public function run()
{
echo "Broom! Broom!\n";
}
}
class Petrol implements Engine
{
public function run()
{
echo "Wroom! Wroom!\n";
}
}
// === EngineBridge ===
// That help us switch engine type
// at run-time through set() method
class EngineBridge
{
public $engine;
public function __construct(Engine $engine)
{
$this->engine = $engine;
}
public function set(Engine $engine)
{
$this->engine = $engine;
}
}
// ---
$diesel = new Diesel();
$petrol = new Petrol();
$bridge = new EngineBridge($diesel);
$bridge->engine->run();
// Output:
// Broom! Broom!
$bridge->set($petrol);
$bridge->engine->run();
// Output:
// Wroom! Wroom!