CSC/ECE 517 Fall 2010/ch5 5c ck

From Expertiza_Wiki
Jump to navigation Jump to search
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.
This occurs when a class is downcast to one of its super classes and a method is called on that super class object.

Generic Example

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.

Single and Multiple Dispatch

Single Dispatch

All Object Oriented languages use a dynamic dispatch process known as single dispatch.
In single dispatch only one object is dynamically at run time.

Multiple Dispatch

All Object Oriented languages use a more complete dynamic dispatch process known as multiple dispatch.

Conclusion

The debate over which is better, class based OOD or prototype based OOD, has been around since the beginning of OOD. Prototype OOD appears to be gaining some mainstream ground, but it is still a long way from taking over traditional class based OOD. It is this authors opinion that this debate is ongoing and will exist in one form or another into the future. I believe that in the past 15 years, neither has proven to be advantagous in all circumstances over the other. I also believe that the evolution of some hybrid language features that attempt to bridge the gap will continue to evolve. I beleive that the future will at times lean in the direction of one or in the direction of the other, bull will never quite cuminate on one or the other.

References

[1] Yahoo Online Dictionary - Prototype