mardi 26 janvier 2016

Does it make sense to use composite pattern in tree view?

I have a tree view where every node is a different type of item - only the label and description property is the same. Does it make sense to represent each of these node item by composite component?

One of the reason I ask is that I have noticed while applying composite design pattern makes it easier to apply aggregate function (applied to aggregate object), it gets somewhat harder to actually access individual items inside that aggregate.

This usually means to access the individual item, it will require type casting the item back to its original type or it needs to contain the type information which is considered a bad practice in OOP. These inner objects are not really exposed anyways in common examples.

To put this in perspective, consider I have TreeView which shows tourist attraction in 3 major cities. The first level of node is the city name, which than contains attractions in that city.

New York
   Time Square
   Central Park
London
   British Meuseum
Paris
   Eiffel Tower

So now when user clicks on city, the program displays information about that city (say in right pane), if they click the attraction, it shows information on that attraction.

Is this a fit for composite design pattern? if so wouldn't it require typecasting the component back to what exactly it is like city or attraction?

Secondly wouldn't that require digging out the component out of the composite to be able to display it? Kind of against the pattern which tends to put them together?

If it matters, my environment is C++/Qt.

Aucun commentaire:

Enregistrer un commentaire