-
Notifications
You must be signed in to change notification settings - Fork 4
/
decorator.php
42 lines (36 loc) · 859 Bytes
/
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
<?php
/**
* Decorator pattern example
*
* @author Christian Bergau <cbergau86@gmail.com>
* @copyright Free for all
* @link http://en.wikipedia.org/wiki/Decorator_pattern
*/
interface ComponentInterface
{
public function operation();
}
class ConcreteComponent implements ComponentInterface
{
public function operation()
{
return 'operation';
}
}
abstract class AbstractDecorator implements ComponentInterface
{
protected $component;
public function __construct(ComponentInterface $component)
{
$this->component = $component;
}
}
class ConcreteDecorator extends AbstractDecorator
{
public function operation()
{
return '[decorated]'.$this->component->operation().'[decorated]';
}
}
$component = new ConcreteDecorator(new ConcreteComponent());
echo $component->operation();