When to use polymorphism?

Recommended Posts

arwi234    100
Hi, i have a question. When using polymorphism is good? For example i have 5 classes: Mesh, Texture, Graphics, Sound and Main(main class of this example). In Graphics there's directx device required for Mesh and Textures, Textures are required for Meshes. My question is: can i use polymorphism in this example (with using Main class as main class xD)?

Share on other sites
oler1s    585
Start by explaining to us what you think polymorphism is.

Share on other sites
Polymorphism is based on inheritance. Inheritance is an is-a relationship.
Think to yourself:
A texture IS-A mesh?
A mesh IS-A graphics?
Etc.
Or maybe an abstract base class; Maybe a Mesh is-a GraphicalObject. Maybe a Texture is-a GraphicalObject. Then you could store them together in a container of GraphicalObjects (I'm not saying you need to or its good to in this situation).

Share on other sites
rip-off    10976
As a rule, polymorphism is overused by those new to it. It isn't usually a massive deal, but it does complicate the code and tends to make large, deep class hierarchies that are messy to work with. Then again, you won't really appreciate the beauty of preferring composition until you've had to work with such monstrosities.

In your example, I'm not sure I'd use any inheritance. Those classes are quite distinct. That said, in some languages you would be forced to write a small interface if you want a "load a resource once and only once" feature, such as a resource cache. That is more a language quirk than anything else.

Share on other sites
arwi234    100
ok i think the best is doing sth like this:

class Gui : protected Graphics //In protected: directx device
etc.
but i have another question with examples:
Ex.1:
class Gui : protected Graphics
{
public:
static void DrawImage();
static void DrawBar();
static void DrawText();
};

Ex.2:
class Gui : protected Graphics
{
public:
static void Draw();
};

and three classes: Image, Bar and Text inherits from Gui. I'm doing programs like in Ex.1 but is Ex.2 better?

Share on other sites
rip-off    10976
Yeah, I can't see what the purpose of the polymorphism is there. That it is protected inheritance indicates that composition would be superior, and the static methods are suspicious.

Why not:
class Gui {public:    void Draw(Graphics &graphics);};class Text {public:    void Draw(Graphics &graphics);};class Image {public:    void Draw(Graphics &graphics);};/* ... and so on ... */

Share on other sites
arwi234    100
so do you think that Ex. 2 is better?

Share on other sites
Ariste    296
Why does GUI inherit from Graphics at all?

Share on other sites
Shannon Barber    1681
The answer is always 'when you need to'.
The objective is to always hit the 'sweet spot' which is code that has the exact complexity that is needed to solve the problem at hand.

Thus, do not use polymorphism unless it is needed to solve the problem.
If you are (or will/would be) juggling function-pointers to make something work, then it's time to look to how polymorphism would make the solution simpler.

There is no great cause for polymorphism in the example you have given.
A few simple classes is fine.

Should you decide to use a 'scene-graph' to render your graphics and audio, then the nodes of the scene graph could use polymorphism.
Visible nodes would have a mesh and interact with the graphics class.
Audible nodes would have a waveform and interact with the audio class.
Both would implement the ISceneNode interface that would contain a method such as 'Intersects(Sphere s)' which would allow you to quickly cull the scene graph using bounding-spheres.
Even then you want to come up with a way to do it to minize the polymorphism in the culling. You want to avoid it in low-level algorithms because a virtual calls translate to poor utilization of the CPU pipeline. e.g. make the SceneNode contain a bounding sphere directly and have a polymorphic pointer to what is 'contained' inside the sphere.
This leads into something slightly different than polymorphism, "generics" or templates. You could write a generic bounding sphere container and then instantiate more than one bounding tree, one for graphics and another for aurals (perhaps a third for solids - physics.)

Once you understand how inheritance and virtual functions work, go get the book 'Design Patterns: Elements of Reusable Object-Oriented Software' to get a better feel on how to use it.

Share on other sites
gtdelarosa2    121
Anytime you are using alot of if-else statements or switch statements, especially if you are duplicating in several places in the code, then you are probably going to need to go with polymorphism. This usually happens when you need to change the functionality of an object based on it's state at runtime.

This is an excellent video that is really enlightening when it comes to polymorphism.