-
Notifications
You must be signed in to change notification settings - Fork 45
/
Bridge.php
56 lines (48 loc) · 1.25 KB
/
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
56
<?php
/**
* Bridge design pattern (example of implementation)
*
* @author Enrico Zimuel ([email protected])
* @see http://en.wikipedia.org/wiki/Bridge_pattern
*/
interface DrawingAPI {
public function drawCircle($x, $y, $radius);
}
class DrawingAPI1 implements DrawingAPI {
public function drawCircle($x, $y, $radius) {
printf ("API1 draw (%d, %d, %d)\n", $x, $y, $radius);
}
}
class DrawingAPI2 implements DrawingAPI {
public function drawCircle($x, $y, $radius) {
printf ("API2 draw (%d, %d, %d)\n", $x, $y, $radius);
}
}
abstract class Shape {
protected $api;
protected $x;
protected $y;
public function __construct(DrawingAPI $api) {
$this->api = $api;
}
}
class CircleShape extends Shape {
protected $radius;
public function __construct($x, $y, $radius, DrawingAPI $api) {
parent::__construct($api);
$this->x = $x;
$this->y = $y;
$this->radius = $radius;
}
public function draw() {
$this->api->drawCircle($this->x, $this->y, $this->radius);
}
}
// Usage example
$shapes = array(
new CircleShape(1, 3, 7, new DrawingAPI1()),
new CircleShape(5, 7, 11, new DrawingAPI2()),
);
foreach ($shapes as $sh) {
$sh->draw();
}