-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorator_pattern.php
78 lines (63 loc) · 1.45 KB
/
decorator_pattern.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
<?php
// allows behavior to be added to an individual object, either statically or dynamically,
// without affecting the behavior of other objects from the same class.
namespace decoratorPattern;
interface BasicCake
{
public function makeCake();
public function deliverCake();
}
class Cake implements BasicCake
{
private $type;
public function __construct($type)
{
$this->type = $type;
}
public function makeCake()
{
echo "making $this->type..." . PHP_EOL;
}
public function deliverCake()
{
echo "your $this->type is ready!" . PHP_EOL;
}
}
abstract class CakeDecorator
{
protected $cake;
public function __construct($cake)
{
$this->cake = $cake;
}
abstract function makeCake();
}
class CakeWithCherry extends CakeDecorator
{
public function makeCake()
{
$this->cake->makeCake();
echo "add cherry..." . PHP_EOL;
}
public function deliverCake()
{
$this->cake->deliverCake();
}
}
class CakeWithHoney extends CakeDecorator
{
public function makeCake()
{
$this->cake->makeCake();
echo "add honey..." . PHP_EOL;
}
public function deliverCake()
{
$this->cake->deliverCake();
}
}
$cheeseCake = new Cake("cheese cake");
$cakeWithCherry = new CakeWithCherry($cheeseCake);
$cakeWithHoney = new CakeWithHoney($cakeWithCherry);
$cakeWithHoney->makeCake();
$cakeWithHoney->deliverCake();