This post originated from an RSS feed registered with Agile Buzz
by Laurent Bossavit.
Original Post: Polymorphism
Feed Title: Incipient(thoughts)
Feed URL: http://bossavit.com/thoughts/index.rdf
Feed Description: You're in a maze of twisty little decisions, all alike. You're in a maze of twisty little decisions, all different.
In every OO class I teach, I introduce encapsulation and polymorphism as the primary OO tactics, with inheritance a distant third. It's hard to tease apart polymorphism and inheritance, because most OO languages are based on an intimate fusion of the two, unnecessarily.
Inheritance is a very strong relationship between objects. It makes it hard to tease anything apart. Some people experience maintenance problems as a result of pervasive inheritance in OO systems. Justifiably, these people then become skeptical about polymorphism as well, because it is so strongly linked to inheritance. They are then tempted to give up on OO altogether. This is throwing the baby out with the bathwater.
My advice as an OO trainer is to use inheritance sparingly, and rely as much as we can on polymorphism.
Polymorphism is an abstraction enabler. The idea is that if we are to describe processes at a relatively high level of abstraction, we are necessarily going to describe in "umbrella" terms a class of situations.
Suppose we are implemeting a Wiki. We might say "One feature of the Wiki is that it logs all edits." Now, "edit" is a nicely abstract term. At a lower level of abstraction, we may well have different kinds of edits: major, minor, spellcheck, file upload, page deletion...
It's a win that we're able to implement the logging feature by writing, in a single place (wherever that is),
for (Edit each in edits) logStream.append("Edit at "+edit.date()+": "+each.debugString())
I have described the type Edit as an "umbrella term"; an abstraction. The type Edit can be an interface in Java terms, or a protocol in Smalltalk terms. No inheritance required, no mutual exclusion implied. One object can perfectly well be an Edit and several other things as well.
What matters is that there are a number of classes of objects which, in a given computation, may play the single role of "Edit". We can say everything we have a need to say about that role in one and only one place. We say nothing about the roles that can played by the very same objects in other computations.
Polymorphism is a well-known phenomenon in molecular biology. A protein folds up for form a "slot" on its 3D surface. Another protein may fit into the first only if it has the proper shape for that slot, called a binding site. Now, more than one protein can have the proper shape to fit into that slot. This mechanism is fundamental to, for instance, vaccination.
In other words, Nature invented polymorphism ages before we rediscovered it in software systems. The designs Nature invents tend to be "smart moves", structures that are so effective that even the undirected, relatively inefficient design process that is evolution will inevitably stumble on them.
Polymorphism is one of these smart moves. Learn how to use it effectively !