Looking for a neat way to accomplish this type problem
I have a design issue which I've had in the past and never really found a satisfactory solution for, so I was hoping to get some fresh ideas on how to handle it.
I have a list of objects. Each object is of a different dervied type, but they all inherit from a common base class.
I have a GUI which needs to show the properties of each object in the list. The exact layout of each section of the GUI depends on the derived class type of the object.
In the past I have basically used typeid(...) and a bunch of conditional branches to determine which layout I use, but this feels clunky to me.
Does anybody have a neat solution they'd be prepared to share?
Cheers,
BJW
Re: Looking for a neat way to accomplish this type problem
typeid is hardly a good solution as it defeats the very idea of polymorphic design. Anyway, IMHO such a design heavily depends on the specific GUI library design you're using, that is the way ui elements are drawn, how do they bind to your controlling classes and so on...
Re: Looking for a neat way to accomplish this type problem
This is just the sort of problem that the Visitor Pattern was devised to solve.
Re: Looking for a neat way to accomplish this type problem
Quote:
typeid is hardly a good solution
Yes. Hence the question.
@John
Thanks for that. From the quick look I just had it seems spot on. I will have a proper look when I get back to work tomorrow.
Re: Looking for a neat way to accomplish this type problem
I wrote an article some time ago on how to implement a templated visitor pattern base class
You may find it useful in creating all the 'boilerplate' code.
Re: Looking for a neat way to accomplish this type problem
Yes, that was a great solution - fits perfectly for what I need. Thanks a bunch!
BJW