Hi, so i found a - in my eyes - nice way to allow code like this:
the idea is to get "multimethod" behaviour by using a visitor pattern like approach:Code:AbstractShape a = new Box(..); AbstractShape b = new Sphere(...); a.TestIntersection(b);
This solution doesn't scale perfectly but i guess its ok. (For each shape a new method must be added to AbstractShape, and for each intersection-test implementation between 2 shapes each shape class must be updated.Code:class AbstractShape { public: bool TestIntersection(AbstractShape &r_other) { return r_other.TestIntersection_(*this); } // By default, an exception is thrown if the method is not overridden virtual bool TestIntersection_(Sphere &) { throw "not implemented"; } virtual bool TestIntersection_(Box &) { throw "not implemented"; } }; class Box : AbstractShape { public: ... bool TestIntersection_(Sphere &) { // call the actual intersection test function } }
The nice thing is, that this solution also allows deriving from a shape. So in my case i have a class OctTreeNode which is derived from Box, and i can still use the TestIntersection method on it.
Does anyone know if there is a name for that design pattern - or does anyone know where a similar pattern was used for intersection tests? I'm sure others figured out that solution, but so far i haven't found what i was looking for.
(Mostly i found ppl who used HashMaps or switch/if blocks, or used template "hacks" (typelists))
I just need something to cite for my work.
Thx![]()



LinkBack URL
About LinkBacks



