-
Notifications
You must be signed in to change notification settings - Fork 0
/
composite.php
57 lines (44 loc) · 925 Bytes
/
composite.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
<?php
interface Component
{
public function execute(): string;
}
class Composite implements Component
{
private array $componentList = [];
public function add(Component $component): void
{
$this->componentList[] = $component;
}
public function execute(): string
{
$string = '';
foreach ($this->componentList as $component) {
$string .= $component->execute() . '<br>';
}
return $string;
}
}
class ComponentA implements Component
{
public function execute(): string
{
return __METHOD__;
}
}
class ComponentB implements Component
{
public function execute(): string
{
return __METHOD__;
}
}
/**
* Client
*/
$componentA = new ComponentA();
$componentB = new ComponentB();
$composite = new Composite();
$composite->add($componentA);
$composite->add($componentB);
echo $composite->execute();