tags:

views:

183

answers:

3

Hi, I'm an experienced C programmer dipping my toes in OO design (specifically C++). I have a particular piece of code I hate and would like to clean up using C++.

The code implements a display tree for use in a 3d graphics app. It is a linked list of entries which have a type field specifying whether the entry is a window, geometry feature or light etc. In particular geometry features can either be a single piece of geometry of a collection of sub features, which is indicated by the presence of a separate structure.

Being a linked list it is a flat structure but the order implies hierarchy. Also, each entry has an attribute structure which is able to be propagated down the list.

A separate function allows this list to be traversed and a provided function pointer to be executed on traversal of the list. Each function used in this way must take proper care to maintain the attribute structure as it is propagated down the list and this is a frequent cause of rendering bugs (usually things not realising they should be redrawn because they are part of a group of entries, for example)

While some aspects of OO design jump right out at me I am interested to hear:

  1. how i would best implement being able to pass a function pointer down a list (or vector - are STL vectors ok for lists like this?)

  2. whether i would be right to implement windows and geometry features as related (or even the same) classes

  3. how to best design a class that can have a collection of itself

  4. any suggestions regarding attribute transferral between objects (e.g color, scale). the desired behaviour is that modifying a feature will alter the attributes of that feature and any sub-features it contains, and modifying a sub-feature will only modify that feature.

I realize this is a long and broad question, so appreciate your thoughts on any of the above queries. thanks!

+2  A: 

1) Use boost::function and stl algorithms like for_each count_if
2) Divide model from the view. It will be ok.
3) GoF design pattern Composite
4) To implement reaction on changes take a look at GoF design pattern Observer

Mykola Golubyev
A: 
  1. you can have your function pointer, but usually in OO every object should know how to handle its data, and there is polymorphism for special c(l)as(s)es
Manrico Corazzi
A: 

To Your Points

1: how i would best implement being able to pass a function pointer down a list (or vector - are STL vectors ok for lists like this?)

Don't. Use an interface (or interfaces) instead. Create a 'Renderable' interface with a 'draw' method that all of your classes can implement. You made need more than one interface, or an interface with a different name and method set.

2: whether i would be right to implement windows and geometry features as related (or even the same) classes

I'm not certain exactly what your "geometry features" are but this may be a case for subclassing. Create a 'Display' class which can be subclassed by your various geometry feature classes and window classes. On the other hand your may be conflating your Model and your View (see MVC).

3: how to best design a class that can have a collection of itself

Probably you want the collection to be of the superclass 'Display' mentioned above (or whatever you choose for your superclass) or possibly even an interface. This is done frequently in GUI frameworks.

4: any suggestions regarding attribute transferral between objects (e.g color, scale). the desired behaviour is that modifying a feature will alter the attributes of that feature and any sub-features it contains, and modifying a sub-feature will only modify that feature

This functionality sounds like it should be a part of a Renderer class. This class would traverse your linked list (or tree) and call the above mentioned 'draw' method on each class. It could also do things like cache attributes from the last ancestor with a given attribute defined. This should allow the sort of attribute or feature overriding you're describing.

In General

While not directly applicable, it would likely be instructive for you to examine an existing OO GUI system. Java and .NET both have several.

Waylon Flinn