Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Double dispatch
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===== Visitor pattern ===== The visitor pattern works by way of a visitor object visiting the elements of a data structure (e.g. list, tree and so on) polymorphically, applying some action (call or agent) against the polymorphic element objects in the visited target structure. In our example below, we make a list of polymorphic SHAPE objects, visiting each of them with a polymorphic SURFACE, asking the SHAPE to be drawn on the SURFACE. <syntaxhighlight lang="eiffel" line highlight="23"> make -- Print shapes on surfaces. local l_shapes: ARRAYED_LIST [SHAPE] l_surfaces: ARRAYED_LIST [SURFACE] do create l_shapes.make (6) l_shapes.extend (create {POLYGON}.make_with_color ("red")) l_shapes.extend (create {RECTANGLE}.make_with_color ("grey")) l_shapes.extend (create {QUADRILATERAL}.make_with_color ("green")) l_shapes.extend (create {PARALLELOGRAM}.make_with_color ("blue")) l_shapes.extend (create {POLYGON}.make_with_color ("yellow")) l_shapes.extend (create {RECTANGLE}.make_with_color ("purple")) create l_surfaces.make (2) l_surfaces.extend (create {ETCHASKETCH}.make) l_surfaces.extend (create {GRAFFITI_WALL}.make) across l_shapes as ic_shapes loop across l_surfaces as ic_surfaces loop ic_surfaces.item.drawing_agent (ic_shapes.item.drawing_data_agent) end end end </syntaxhighlight> We start by creating a collection of SHAPE and SURFACE objects. We then iterate over one of the lists (SHAPE), allowing elements of the other (SURFACE) to visit each of them in turn. In the example code above, SURFACE objects are visiting SHAPE objects. The code makes a polymorphic call on {SURFACE}.draw indirectly by way of the `drawing_agent', which is the first call (dispatch) of the double-dispatch pattern. It passes an indirect and polymorphic agent (`drawing_data_agent'), allowing our visitor code to only know about two things: * What is the drawing agent of the surface (e.g. al_surface.drawing_agent on line #21)? * What is the drawing data agent of the shape (e.g. al_shape.drawing_data_agent on line #21)? Because both SURFACE and SHAPE define their own agents, our visitor code is freed from having to know what is the appropriate call to make, polymorphically or otherwise. This level of indirection and decoupling is simply not achievable in other common languages like C, C++ and Java except through either some form of [[reflection (computer science)|reflection]] or feature overloading with signature matching.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)