|
-
April 27th, 2010, 02:19 AM
#1
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
-
April 27th, 2010, 02:36 AM
#2
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...
-
April 27th, 2010, 02:41 AM
#3
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.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
April 27th, 2010, 07:14 AM
#4
Re: Looking for a neat way to accomplish this type problem
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.
-
April 27th, 2010, 07:43 AM
#5
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.
"It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
Richard P. Feynman
-
April 28th, 2010, 04:24 PM
#6
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|