-
Notifications
You must be signed in to change notification settings - Fork 0
/
visitor.php
58 lines (44 loc) · 1.26 KB
/
visitor.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
<?php
interface Element
{
public function accept(Visitor $visitor): string;
}
interface Visitor
{
public function visitElementImplementationA(ElementImplementationA $element): string;
public function visitElementImplementationB(ElementImplementationB $element): string;
}
class ElementImplementationA implements Element
{
public function accept(Visitor $visitor): string
{
return $visitor->visitElementImplementationA($this);
}
}
class ElementImplementationB implements Element
{
public function accept(Visitor $visitor): string
{
return $visitor->visitElementImplementationB($this);
}
}
class VisitorImplementationA implements Visitor
{
public function visitElementImplementationA(ElementImplementationA $element): string
{
return __METHOD__;
}
public function visitElementImplementationB(ElementImplementationB $element): string
{
return __METHOD__;
}
}
/**
* Client
*/
$elementImplementationA = new ElementImplementationA();
$visitorImplementationA = new VisitorImplementationA();
echo $elementImplementationA->accept($visitorImplementationA);
echo '<br>';
$elementImplementationB = new ElementImplementationB();
echo $elementImplementationB->accept($visitorImplementationA);