-
Notifications
You must be signed in to change notification settings - Fork 4
/
visitor.php
158 lines (138 loc) · 3.14 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* Visitor pattern example
*
* @author Daniel Basten <[email protected]>
* @copyright Free for all
* @author Design Patterns. Elements of Reusable Object-Oriented Software.
* Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
*/
interface Visitor
{
/**
* @param ConcreteElementA $concreteElementA
* @return void
*/
public function VisitA(ConcreteElementA $concreteElementA);
/**
* @param ConcreteElementB $concreteElementB
* @return void
*/
public function VisitB(ConcreteElementB $concreteElementB);
}
class concreteVisitorA implements Visitor
{
/**
* @param ConcreteElementA $concreteElementA
*/
public function VisitA(ConcreteElementA $concreteElementA)
{
$concreteElementA->setCounter(
$concreteElementA->getCounter() + 1
);
}
/**
* @param ConcreteElementB $concreteElementB
*/
public function VisitB(ConcreteElementB $concreteElementB)
{
$concreteElementB->setName(
'visited ' . $concreteElementB->getName()
);
}
}
class concreteVisitorB implements Visitor
{
/**
* @param ConcreteElementA $concreteElementA
*/
public function VisitA(ConcreteElementA $concreteElementA)
{
echo 'counter: ' . $concreteElementA->getCounter() . PHP_EOL;
}
/**
* @param ConcreteElementB $concreteElementB
*/
public function VisitB(ConcreteElementB $concreteElementB)
{
echo 'name: ' . $concreteElementB->getName() . PHP_EOL;
}
}
interface Element
{
/**
* @param Visitor $visitor
* @return void
*/
public function host(Visitor $visitor);
}
class ConcreteElementA implements Element
{
/**
* @var int
*/
private $counter = 0;
/**
* @param Visitor $visitor
* @return void
*/
public function host(Visitor $visitor)
{
$visitor->VisitA($this);
}
/**
* @param int $counter
*/
public function setCounter($counter)
{
$this->counter = $counter;
}
/**
* @return int
*/
public function getCounter()
{
return $this->counter;
}
}
class ConcreteElementB implements Element
{
/**
* @var string
*/
private $name = 'neat FooBar Object';
/**
* @param Visitor $visitor
* @return void
*/
public function host(Visitor $visitor)
{
$visitor->VisitB($this);
}
/**
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getName()
{
return $this->name;
}
}
$concreteElementA = new ConcreteElementA();
$concreteElementB = new ConcreteElementB();
// this one changes object's data
$concreteVisitorA = new concreteVisitorA();
// this one outputs the object's data
$concreteVisitorB = new concreteVisitorB();
// let modifying visitors run
$concreteElementA->host($concreteVisitorA);
$concreteElementB->host($concreteVisitorA);
// now our reading visitors
$concreteElementA->host($concreteVisitorB);
$concreteElementB->host($concreteVisitorB);