<s>
In	O
object-oriented	B-Language
programming	I-Language
and	O
software	B-General_Concept
engineering	I-General_Concept
,	O
the	O
visitor	B-Language
design	I-Language
pattern	I-Language
is	O
a	O
way	O
of	O
separating	O
an	O
algorithm	O
from	O
an	O
object	O
structure	O
on	O
which	O
it	O
operates	O
.	O
</s>
<s>
In	O
essence	O
,	O
the	O
visitor	B-Language
allows	O
adding	O
new	O
virtual	B-Application
functions	I-Application
to	O
a	O
family	O
of	O
classes	O
,	O
without	O
modifying	O
the	O
classes	O
.	O
</s>
<s>
Instead	O
,	O
a	O
visitor	B-Language
class	O
is	O
created	O
that	O
implements	O
all	O
of	O
the	O
appropriate	O
specializations	O
of	O
the	O
virtual	B-Application
function	I-Application
.	O
</s>
<s>
The	O
visitor	B-Language
takes	O
the	O
instance	O
reference	O
as	O
input	O
,	O
and	O
implements	O
the	O
goal	O
through	O
double	B-Language
dispatch	I-Language
.	O
</s>
<s>
Programming	O
languages	O
with	O
sum	B-Language
types	I-Language
and	O
pattern	B-Language
matching	I-Language
obviate	O
many	O
of	O
the	O
benefits	O
of	O
the	O
visitor	B-Language
pattern	I-Language
,	O
as	O
the	O
visitor	B-Language
class	O
is	O
able	O
to	O
both	O
easily	O
branch	O
on	O
the	O
type	O
of	O
the	O
object	O
and	O
generate	O
a	O
compiler	O
error	O
if	O
a	O
new	O
object	O
type	O
is	O
defined	O
which	O
the	O
visitor	B-Language
does	O
not	O
yet	O
handle	O
.	O
</s>
<s>
that	O
describe	O
how	O
to	O
solve	O
recurring	O
design	O
problems	O
to	O
design	O
flexible	O
and	O
reusable	O
object-oriented	B-Language
software	O
,	O
that	O
is	O
,	O
</s>
<s>
Define	O
a	O
separate	O
(	O
visitor	B-Language
)	O
object	O
that	O
implements	O
an	O
operation	O
to	O
be	O
performed	O
on	O
elements	O
of	O
an	O
object	O
structure	O
.	O
</s>
<s>
Clients	O
traverse	O
the	O
object	O
structure	O
and	O
call	O
a	O
dispatching	O
operation	O
accept	O
(	O
visitor	B-Language
)	O
on	O
an	O
element	O
—	O
that	O
"	O
dispatches	O
"	O
(	O
delegates	O
)	O
the	O
request	O
to	O
the	O
"	O
accepted	O
visitor	B-Language
object	O
"	O
.	O
</s>
<s>
The	O
visitor	B-Language
object	O
then	O
performs	O
the	O
operation	O
on	O
the	O
element	O
(	O
"	O
visits	O
the	O
element	O
"	O
)	O
.	O
</s>
<s>
by	O
adding	O
new	O
visitor	B-Language
objects	O
.	O
</s>
<s>
See	O
also	O
the	O
UML	B-Language
class	O
and	O
sequence	O
diagram	O
below	O
.	O
</s>
<s>
The	O
Gang	O
of	O
Four	O
defines	O
the	O
Visitor	B-Language
as	O
:	O
</s>
<s>
The	O
nature	O
of	O
the	O
Visitor	B-Language
makes	O
it	O
an	O
ideal	O
pattern	O
to	O
plug	O
into	O
public	O
APIs	O
,	O
thus	O
allowing	O
its	O
clients	O
to	O
perform	O
operations	O
on	O
a	O
class	O
using	O
a	O
"	O
visiting	O
"	O
class	O
without	O
having	O
to	O
modify	O
the	O
source	O
.	O
</s>
<s>
A	O
drawback	O
to	O
this	O
pattern	O
,	O
however	O
,	O
is	O
that	O
it	O
makes	O
extensions	O
to	O
the	O
class	O
hierarchy	O
more	O
difficult	O
,	O
as	O
new	O
classes	O
typically	O
require	O
a	O
new	O
visit	O
method	O
to	O
be	O
added	O
to	O
each	O
visitor	B-Language
.	O
</s>
<s>
Consider	O
the	O
design	O
of	O
a	O
2D	O
computer-aided	B-Application
design	I-Application
(	O
CAD	B-Application
)	O
system	O
.	O
</s>
<s>
Instead	O
,	O
the	O
visitor	B-Language
pattern	I-Language
can	O
be	O
applied	O
.	O
</s>
<s>
In	O
the	O
CAD	B-Application
example	O
,	O
each	O
save	O
function	O
would	O
be	O
implemented	O
as	O
a	O
separate	O
Visitor	B-Language
subclass	O
.	O
</s>
<s>
The	O
visitor	B-Language
pattern	I-Language
may	O
be	O
used	O
for	O
iteration	B-Algorithm
over	O
container-like	O
data	O
structures	O
just	O
like	O
Iterator	B-Language
pattern	I-Language
but	O
with	O
limited	O
functionality	O
.	O
</s>
<s>
For	O
example	O
,	O
iteration	B-Algorithm
over	O
a	O
directory	O
structure	O
could	O
be	O
implemented	O
by	O
a	O
function	O
class	O
instead	O
of	O
more	O
conventional	O
loop	O
pattern	O
.	O
</s>
<s>
This	O
would	O
allow	O
deriving	O
various	O
useful	O
information	O
from	O
directories	O
content	O
by	O
implementing	O
a	O
visitor	B-Language
functionality	O
for	O
every	O
item	O
while	O
reusing	O
the	O
iteration	B-Algorithm
code	O
.	O
</s>
<s>
It	O
's	O
widely	O
employed	O
in	O
Smalltalk	B-Application
systems	O
and	O
can	O
be	O
found	O
in	O
C++	B-Language
as	O
well	O
.	O
</s>
<s>
A	O
drawback	O
of	O
this	O
approach	O
,	O
however	O
,	O
is	O
that	O
you	O
ca	O
n't	O
break	O
out	O
of	O
the	O
loop	O
easily	O
or	O
iterate	B-Algorithm
concurrently	O
(	O
in	O
parallel	O
i.e.	O
</s>
<s>
traversing	O
two	O
containers	B-Application
at	O
the	O
same	O
time	O
by	O
a	O
single	O
variable	O
)	O
.	O
</s>
<s>
The	O
latter	O
would	O
require	O
writing	O
additional	O
functionality	O
for	O
a	O
visitor	B-Language
to	O
support	O
these	O
features	O
.	O
</s>
<s>
In	O
the	O
UML	B-Language
class	O
diagram	O
above	O
,	O
the	O
ElementA	O
class	O
does	O
n't	O
implement	O
a	O
new	O
operation	O
directly	O
.	O
</s>
<s>
Instead	O
,	O
ElementA	O
implements	O
a	O
dispatching	O
operation	O
accept(visitor )	O
that	O
"	O
dispatches	O
"	O
(	O
delegates	O
)	O
a	O
request	O
to	O
the	O
"	O
accepted	O
visitor	B-Language
object	O
"	O
(visitor.visitElementA(this )	O
)	O
.	O
</s>
<s>
ElementB	O
then	O
implements	O
accept(visitor )	O
by	O
dispatching	O
to	O
visitor.visitElementB(this )	O
.	O
</s>
<s>
shows	O
the	O
run-time	O
interactions	O
:	O
The	O
Client	O
object	O
traverses	O
the	O
elements	O
of	O
an	O
object	O
structure	O
(	O
ElementA	O
,	O
ElementB	O
)	O
and	O
calls	O
accept(visitor )	O
on	O
each	O
element	O
.	O
</s>
<s>
ElementA	O
,	O
which	O
calls	O
visitElementA(this )	O
on	O
the	O
accepted	O
visitor	B-Language
object	O
.	O
</s>
<s>
ElementB	O
,	O
which	O
calls	O
visitElementB(this )	O
on	O
the	O
visitor	B-Language
that	O
"	O
visits	O
"	O
ElementB	O
(	O
calls	O
operationB( )	O
)	O
.	O
</s>
<s>
The	O
visitor	B-Language
pattern	I-Language
requires	O
a	O
programming	O
language	O
that	O
supports	O
single	O
dispatch	O
,	O
as	O
common	O
object-oriented	B-Language
languages	I-Language
(	O
such	O
as	O
C++	B-Language
,	O
Java	B-Language
,	O
Smalltalk	B-Application
,	O
Objective-C	B-Language
,	O
Swift	B-Application
,	O
JavaScript	B-Language
,	O
Python	B-Language
and	O
C#	B-Application
)	O
do	O
.	O
</s>
<s>
Under	O
this	O
condition	O
,	O
consider	O
two	O
objects	O
,	O
each	O
of	O
some	O
class	O
type	O
;	O
one	O
is	O
termed	O
the	O
element	O
,	O
and	O
the	O
other	O
is	O
visitor	B-Language
.	O
</s>
<s>
The	O
visitor	B-Language
declares	O
a	O
visit	O
method	O
,	O
which	O
takes	O
the	O
element	O
as	O
an	O
argument	O
,	O
for	O
each	O
class	O
of	O
element	O
.	O
</s>
<s>
Concrete	O
visitors	O
are	O
derived	O
from	O
the	O
visitor	B-Language
class	O
and	O
implement	O
these	O
visit	O
methods	O
,	O
each	O
of	O
which	O
implements	O
part	O
of	O
the	O
algorithm	O
operating	O
on	O
the	O
object	O
structure	O
.	O
</s>
<s>
The	O
state	O
of	O
the	O
algorithm	O
is	O
maintained	O
locally	O
by	O
the	O
concrete	O
visitor	B-Language
class	O
.	O
</s>
<s>
The	O
element	O
declares	O
an	O
accept	O
method	O
to	O
accept	O
a	O
visitor	B-Language
,	O
taking	O
the	O
visitor	B-Language
as	O
an	O
argument	O
.	O
</s>
<s>
In	O
its	O
simplest	O
form	O
,	O
this	O
is	O
no	O
more	O
than	O
a	O
call	O
to	O
the	O
visitor	B-Language
's	O
visit	O
method	O
.	O
</s>
<s>
Composite	O
elements	O
,	O
which	O
maintain	O
a	O
list	O
of	O
child	O
objects	O
,	O
typically	O
iterate	B-Algorithm
over	O
these	O
,	O
calling	O
each	O
child	O
's	O
accept	O
method	O
.	O
</s>
<s>
When	O
an	O
operation	O
is	O
to	O
be	O
performed	O
which	O
is	O
implemented	O
using	O
the	O
Visitor	B-Language
pattern	I-Language
,	O
it	O
calls	O
the	O
accept	O
method	O
of	O
the	O
top-level	O
element(s )	O
.	O
</s>
<s>
When	O
the	O
accept	O
method	O
is	O
called	O
in	O
the	O
program	O
,	O
its	O
implementation	O
is	O
chosen	O
based	O
on	O
both	O
the	O
dynamic	O
type	O
of	O
the	O
element	O
and	O
the	O
static	O
type	O
of	O
the	O
visitor	B-Language
.	O
</s>
<s>
When	O
the	O
associated	O
visit	O
method	O
is	O
called	O
,	O
its	O
implementation	O
is	O
chosen	O
based	O
on	O
both	O
the	O
dynamic	O
type	O
of	O
the	O
visitor	B-Language
and	O
the	O
static	O
type	O
of	O
the	O
element	O
,	O
as	O
known	O
from	O
within	O
the	O
implementation	O
of	O
the	O
accept	O
method	O
,	O
which	O
is	O
the	O
same	O
as	O
the	O
dynamic	O
type	O
of	O
the	O
element	O
.	O
</s>
<s>
(	O
As	O
a	O
bonus	O
,	O
if	O
the	O
visitor	B-Language
ca	O
n't	O
handle	O
an	O
argument	O
of	O
the	O
given	O
element	O
's	O
type	O
,	O
then	O
the	O
compiler	O
will	O
catch	O
the	O
error	O
.	O
)	O
</s>
<s>
Thus	O
,	O
the	O
implementation	O
of	O
the	O
visit	O
method	O
is	O
chosen	O
based	O
on	O
both	O
the	O
dynamic	O
type	O
of	O
the	O
element	O
and	O
the	O
dynamic	O
type	O
of	O
the	O
visitor	B-Language
.	O
</s>
<s>
This	O
effectively	O
implements	O
double	B-Language
dispatch	I-Language
.	O
</s>
<s>
For	O
languages	O
whose	O
object	B-Language
systems	I-Language
support	O
multiple	O
dispatch	O
,	O
not	O
only	O
single	O
dispatch	O
,	O
such	O
as	O
Common	B-Language
Lisp	I-Language
or	O
C#	B-Application
via	O
the	O
Dynamic	B-Application
Language	I-Application
Runtime	I-Application
(	O
DLR	O
)	O
,	O
implementation	O
of	O
the	O
visitor	B-Language
pattern	I-Language
is	O
greatly	O
simplified	O
(	O
a.k.a.	O
</s>
<s>
Dynamic	O
Visitor	B-Language
)	O
by	O
allowing	O
use	O
of	O
simple	O
function	O
overloading	O
to	O
cover	O
all	O
the	O
cases	O
being	O
visited	O
.	O
</s>
<s>
A	O
dynamic	O
visitor	B-Language
,	O
provided	O
it	O
operates	O
on	O
public	O
data	O
only	O
,	O
conforms	O
to	O
the	O
open/closed	O
principle	O
(	O
since	O
it	O
does	O
not	O
modify	O
extant	O
structures	O
)	O
and	O
to	O
the	O
single	O
responsibility	O
principle	O
(	O
since	O
it	O
implements	O
the	O
Visitor	B-Language
pattern	I-Language
in	O
a	O
separate	O
component	O
)	O
.	O
</s>
<s>
The	O
visitor	B-Language
here	O
is	O
then	O
the	O
object	O
,	O
not	O
the	O
stream	O
.	O
</s>
<s>
The	O
following	O
example	O
is	O
in	O
the	O
language	O
Java	B-Language
,	O
and	O
shows	O
how	O
the	O
contents	O
of	O
a	O
tree	O
of	O
nodes	O
(	O
in	O
this	O
case	O
describing	O
the	O
components	O
of	O
a	O
car	O
)	O
can	O
be	O
printed	O
.	O
</s>
<s>
Instead	O
of	O
creating	O
print	O
methods	O
for	O
each	O
node	O
subclass	O
(	O
Wheel	O
,	O
Engine	O
,	O
Body	O
,	O
and	O
Car	O
)	O
,	O
one	O
visitor	B-Language
class	O
(	O
CarElementPrintVisitor	O
)	O
performs	O
the	O
required	O
printing	O
action	O
.	O
</s>
