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!
==== Setup ==== Before looking at SHAPE or SURFACE, we need to examine the high level decoupled use of our double-dispatch. ===== 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. ===== SURFACE ===== Within the polymorphic call to {SURFACE}.draw is the call to an agent, which becomes the second polymorphic call or dispatch in the double-dispatch pattern. <syntaxhighlight lang="eiffel" line> deferred class SURFACE feature {NONE} -- Initialization make -- Initialize Current. do drawing_agent := agent draw end feature -- Access drawing_agent: PROCEDURE [ANY, TUPLE [STRING, STRING]] -- Drawing agent of Current. feature {NONE} -- Implementation draw (a_data_agent: FUNCTION [ANY, TUPLE, TUPLE [name, color: STRING]]) -- Draw `a_shape' on Current. local l_result: TUPLE [name, color: STRING] do l_result := a_data_agent (Void) print ("draw a " + l_result.color + " " + l_result.name + " on " + type + "%N") end type: STRING -- Type name of Current. deferred end end </syntaxhighlight> The agent argument in line #19 and call in line #24 are both polymorphic and decoupled. The agent is decoupled because the {SURFACE}.draw feature has no idea what class `a_data_agent' is based on. There is no way to tell what class the operation agent was derived from, so it does not have to come from SHAPE or one of its descendants. This is a distinct advantage of Eiffel agents over the single inheritance, dynamic and polymorphic binding of other languages. The agent is dynamically polymorphic at run-time because the object is created in the moment it is needed, dynamically, where the version of the objectified routine is determined at that time. The only strongly bound knowledge is of the Result type of the agent signature—that is—a named TUPLE with two elements. However, this specific requirement is based on a demand of the enclosing feature (e.g. line #25 uses the named elements of the TUPLE to fulfill `draw' feature of SURFACE), which is necessary and has not been avoided (and perhaps cannot be). Finally, note how only the `drawing_agent' feature is exported to ANY client! This means that the visitor pattern code (who is the ONLY client of this class) only needs to know about the agent to get its job done (e.g. using the agent as the feature applied to the visited objects). ===== SHAPE ===== The SHAPE class has the basis (e.g. drawing data) for what is drawn, perhaps on a SURFACE, but it does not have to be. Again, the agents provide the indirection and class agnostics required to make the co-variant relationship with SHAPE as decoupled as possible. Additionally, please take note of the fact that SHAPE only provides `drawing_data_agent' as a fully exported feature to any client. Therefore, the only way to interact with SHAPE, other than creation, is through the facilities of the `drawing_data_agent', which is used by ANY client to indirectly and polymorphically gather drawing data for the SHAPE! <syntaxhighlight lang="eiffel" line> deferred class SHAPE feature {NONE} -- Initialization make_with_color (a_color: like color) -- Make with `a_color' as `color'. do color := a_color drawing_data_agent := agent drawing_data ensure color_set: color.same_string (a_color) end feature -- Access drawing_data_agent: FUNCTION [ANY, TUPLE, like drawing_data] -- Data agent for drawing. feature {NONE} -- Implementation drawing_data: TUPLE [name: like name; color: like color] -- Data needed for drawing of Current. do Result := [name, color] end name: STRING -- Object name of Current. deferred end color: STRING -- Color of Current. end </syntaxhighlight>
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)