Article Preview
Buy Now
COLUMN
Double Dispatch
Better Design Through Indirection
Issue: 1.6 (June/July 2003)
Author: Charles Yeomans
Author Bio: Charles Yeomans is a software developer in Lexington, Kentucky.
Article Description: No description available.
Article Length (in bytes): 5,479
Starting Page Number: 36
Article Number: 1618
Resource File(s):
1618.zip Updated: 2013-03-11 19:07:56
Related Link(s): None
Excerpt of article text...
You're writing a spaceship game in which ships of various types collide. Let's suppose that the game currently has three types of ships: ShipA, ShipB, and ShipC, all of which inherit from a common abstract class SpaceShip. SpaceShip has an abstract function CollideWith(ship as SpaceShip) as CollisionOutcome. How do you implement ShipA, ShipB, and ShipC?
It's immediately clear that there are six possible collisions, and it's certainly possible that the code in each situation will be quite different. Our first step will be to move collision resolution into its own class, SpaceshipCollisionResolver, and give it a single method, Resolve, that we'll overload to handle all possible cases.
...End of Excerpt. Please purchase the magazine to read the full article.