forked from eimg/design-patterns-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path7-decorator.php
62 lines (51 loc) · 1.15 KB
/
7-decorator.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
<?php
interface CarInterface
{
public function setup();
}
class Car implements CarInterface
{
public $features;
public function setup()
{
$this->features = "Alloy Wheel";
}
}
abstract class CarDecorator implements CarInterface
{
protected $car;
public function __construct(CarInterface $car)
{
$this->car = $car;
}
abstract public function setup();
}
// === A CarDecorator that can add ===
// additional feature to existing Car object
class SunroofDecorator extends CarDecorator
{
public function setup()
{
$this->car->setup();
$this->car->features .= ", Sunroof";
$this->features = $this->car->features;
}
}
// === A CarDecorator that can add ===
// additional feature to existing Car object
class SpoilerDecorator extends CarDecorator
{
public function setup()
{
$this->car->setup();
$this->car->features .= ", Spoiler";
$this->features = $this->car->features;
}
}
// ---
$car = new Car();
$car = new SunroofDecorator($car);
$car = new SpoilerDecorator($car);
$car->setup();
echo $car->features;
// Output: Alloy Wheel, Sunroof, Spoiler