views:

152

answers:

6

Hi,

I have a question about a problem I'm struggling with. Hope you can bear with me.

Imagine I have an Object class representing the base class of a hierarchy of physical objects. Later I inherit from it to create an Object1D, Object2D and Object3D classes. Each of these derived classes will have some specific methods and attributes. For example, the 3d object might have functionality to download a 3d model to be used by a renderer.

So I'd have something like this:

class Object {};
class Object1D : public Object { Point mPos; };
class Object2D : public Object { ... };
class Object3D : public Object { Model mModel; };

Now I'd have a separate class called Renderer, which simply takes an Object as argument and well, renders it :-) In a similar way, I'd like to support different kinds of renderers. For instance, I could have a default one that every object could rely on, and then provide other specific renderers for some kind of objects:

class Renderer {};  // Default one
class Renderer3D : public Renderer {};

And here comes my problem. A renderer class needs to get an Object as an argument, for example in the constructor in order to retrieve whatever data it needs to render the object.

So far so good. But a Renderer3D would need to get an Object3D argument, in order to get not only the basic attributes but also the specific attributes of a 3d object.

Constructors would look like this:

CRenderer(Object& object);
CRenderer3D(Object3D& object);

Now how do I specify this in a generic way? Or better yet, is there a better way to design this?

I know I could rely on RTTI or similar but I'd like to avoid this if possible as I feel there is probably a better way to deal with this.

Thanks in advance!

+4  A: 

One way to deal with this coupling is to use factories. A generic factory creates objects and renderers. A 2D factory creates 2D objects and compatible 2D renderers, a 3D factory creates 3D objects and compatible 3D renderers.

abstract class Factory {
  abstract Object createObject();
  abstract Renderer createRenderer();
}

class 2DFactory {
  Object createObject() { return new 2DObject(); }
  Renderer createRenderer() { return new 2DRenderer(); }
}

// similarly for 3D

class Client {
  Client(Factory factory) { ... }
  void render() {
    factory.createRenderer().render(factory.createObject());
  }
}

Thus if you don't mix up products from different factories, you can keep your client code clean. If the client code deals with only one family of objects & renderers at a time, it is trivial to pass it the proper factory, then let it use objects and renderers without knowing their concrete type. A variation on this is to let the object create a compatible renderer via a factory method, as suggested by Jason.

Another possibility would be to use templates/generics, but this is language specific. Your implementation language looks like C++, so I venture to this direction. You could use template specialization:

template<class T> class Renderer {
  void render(T object) { ... }
};

template<> class Renderer<Object3D> {
  void render(Object3D object) {
    // render the 3D way
  }
};

// similarly for 2D
Péter Török
Thanks Péter, see my comment bellow.
Dan
+1  A: 

Is this doable?

template<typename T> class Renderer {};
class Renderer3D : public Renderer<Object3D> {};
dave
Templates might be an option, though I'm not sure how it would fit with my plugin idea.. Need to do some research. Thanks.
Dan
+1  A: 

It sounds like you're looking for the double dispatch pattern. To use this, you add an abstract method to your base class, something like this:

Dispatch(renderer)

Each subclass of Object then overrides Dispatch and calls the appropriate method on the provided renderer for its type:

Object2D.Dispatch(renderer)
{
   renderer.Render2D(self)
}

You can also use interfaces to make this more generic. Create an interface for rendering each type of object and have your dispatch method check if the correct interface is supported:

Object2D.Dispatch(renderer)
{
    render2d = render as IRender2D
    if render2d found
        render2d.Render(self)
}
Peter Ruderman
Right, so in this case each Renderer would have a different function.. I couldn't use a generic base class, but there doesn't seem to be another way..
Dan
+3  A: 

One approach would be to allow the Objects to create their own Renderer's though a getRenderer() method which returns a Renderer of the appropriate type. This is an example of the Factory Method pattern. Depending on the features of your implementation language, the getRenderer method could be an abstract method in your parent Object class with return an instance of a parent Renderer class/interface, which the specific Renderers extend/implement.

Jason Jenkins
Thanks Jason, see my comment bellow.
Dan
A: 

Here's another way to look at this. Make renderer a part of your object. You can assign the right kind of renderer to each object you create and on each object you would have a

Render(){
    myRenderer.Render(this)
}
derdo
A: 
Dan