The Visitor design pattern provides a solution:
Define a separate
Visitor
object that implements operations
to be performed
on the elements of
an object structure.
Clients traverse the object structure
and call
accept(visitor) on each element
to delegate the request
to the "accepted visitor object".
The visitor object then performs the request
("visits" the element).
Describing the Visitor design in more detail is the theme
of the following sections.
See Applicability section for all problems Visitor can solve.
accept(visitor)
for each Element class
(see also Collaboration and Implementation).
Visitor
objects:
Element classes,
define a common Visitor interface
by defining a "visit" operation for
each
Element class
(Visitor | visitElementA(e),visitElementB(e),…).
Visitor1,…)
that implement the Visitor interface.