Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about design (inheritance, polymorphism)

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!

like image 477
Dan Avatar asked Jan 23 '23 07:01

Dan


2 Answers

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
like image 94
Péter Török Avatar answered Jan 24 '23 20:01

Péter Török


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.

like image 32
Jason Jenkins Avatar answered Jan 24 '23 20:01

Jason Jenkins