views:

128

answers:

4

Disclaimer: I'm trying to learn proper OO programming/design, so I'm pretty new to this stuff.

I guess this is a general design patterns question, but I'll base my example on a game engine or something that renders objects to the display.

Consider the following:

hierarchy

How can this sort of separation between physical objects (e.g., cubes, spheres, etc.) and the rendering mechanism be achieved in an extensible manner?

This design is not set in stone, and perhaps I've got something wrong from the start. I'm just curious as to how a problem like this is solved in real world code.

+3  A: 

That would be the Adapter pattern, or it could be implemented as a Strategy pattern.

Ignacio Vazquez-Abrams
+1  A: 

The renderer should not be extended by the objects which he is supposed to draw. (Just my opinion) an object in your world is NOT a renderer but the renderer uses objects.

So you have maybe:

Interface IRenderer which defines a function draw(BasicObject).

Then your objects just extend BasicObject to be handled by the/a renderer.

As I said just my opinion. :)

InsertNickHere
A: 

Strategy patern it is.

portland
A: 

I would use a Visitor pattern here.

Where the Visitor is the renderer and were the Visited is the 3D/Object. I would also make the 3D/Object a composite.

mathk