CSC/ECE 517 Fall 2010/ch5 5c ck: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 35: Line 35:
==Single and Multiple Dispatch==
==Single and Multiple Dispatch==
===Single Dispatch===
===Single Dispatch===
At a minimum, all Object Oriented languages immplement a dynamic dispatch process known as single dispatch.<br/>
At a minimum, all Object Oriented languages implement a dynamic dispatch process known as single dispatch.<br/>
In single a method call considers the dynamic type of only the class that the message is sent to.
In single dispatch a method call considers the dynamic type of only the class that the message is sent to.
The source argument is the only one that is accessed at run time, all parameters are treated as static types.
The source argument is the only one that is accessed at run time, all parameters are treated as static types.


===Multiple Dispatch===
===Multiple Dispatch===
A few Object Oriented languages use a more complete dynamic dispatch process known as multiple dispatch.<br/>
A few Object Oriented languages implement a more complete dynamic dispatch process known as multiple dispatch.<br/>
In multiple dispatch more than one or all objects are accessed dynamically at run time.
In multiple dispatch more than one or all objects are accessed dynamically at run time.
The source object is accessed at run time, and one or more parameters are dynamically accessed at run time.
The source object is accessed at run time, and one or more parameters are dynamically accessed at run time.
===Double Dispatch===
This method uses single dispatch and the [http://en.wikipedia.org/wiki/Visitor_pattern visitor pattern][2] to simulate a special form of multiple dispatch where the run time types of two arguments are taken into account.


==Performance==
==Performance==

Revision as of 20:46, 30 October 2010

Dynamic Dispatch

Introduction

Dynamic dispatch means there is a determination of which method to execute at run time, when a class and one of it's subclasses have the same method signature. Evaluation of Control Structures for Dynamic Dispatch in JAVA (pg 5)[1]
Dynamic dispatch occurs when a class is downcast to one of its super classes and a method is called on that super class object.

Generic Example

Using Dynamic Dispatch

We have a set of objects that can be rendered to the display.
Each object implements the IRenderable interface as seen below:



Utilizing the ability of dynamic dispatch it is very simple to render a collection of IRenderable[] objects for the display:

IRenderable[] irCollection = {new Circle(0,0,.5), new Polygon(0,0,1,1,2,2,0,0)}
foreach (IRenderable ir in irCollection)
{
   ir.render();
}

Note: This example would be the same if IRenderable were a class or abstract class object.<br/

Static Simulation of Dynamic Dispatch

Without dynamic dispatch we would have to test the type of the object, cast it to a subtype and make the call.

IRenderable[] irCollection = {new Circle(0,0,.5), new Polygon(0,0,1,1,2,2,0,0)}
foreach (IRenderable ir in irCollection)
{
   if (ir is Circle) ((Circle)ir).render();
   if (ir is Polygon) ((Polygon)ir).render();
}

This is more complicated than using dynamic dispatch and it also requires us to alter this code every time we add a new class.

Single and Multiple Dispatch

Single Dispatch

At a minimum, all Object Oriented languages implement a dynamic dispatch process known as single dispatch.
In single dispatch a method call considers the dynamic type of only the class that the message is sent to. The source argument is the only one that is accessed at run time, all parameters are treated as static types.

Multiple Dispatch

A few Object Oriented languages implement a more complete dynamic dispatch process known as multiple dispatch.
In multiple dispatch more than one or all objects are accessed dynamically at run time. The source object is accessed at run time, and one or more parameters are dynamically accessed at run time.

Double Dispatch

This method uses single dispatch and the visitor pattern[2] to simulate a special form of multiple dispatch where the run time types of two arguments are taken into account.

Performance

According to Technical Report on C++ Performance (pg 26)[4] "Calling a virtual function is roughly equivalent to calling a function through a pointer stored in an array"

Conclusion

References

[1] Evaluation of Control Structures for Dynamic Dispatch in Java by Inira Lorraine and Karel Drieson(pg 5)
[4] Technical Report on C++ Performance ISO/IEC TR 18015:2006(E) 2006-02-15 (pg. 26)