tags:

views:

53

answers:

2

Hi,

I have a problem where the data is represented in form of a tree (Ex: binary tree). The tree has nodes of different kind. So basically, there is a base class and then different nodes are derived from it.

This data also needs to be presented on the screen (to the user) and this involves custom drawing of each nodes. How the node is drawn depends on the type of the node. The tree also involves simplification process using which a new simplified form of the original tree is derived based on some rules.

In order to draw these nodes I need to maintain the position and size. I'd like to know how I can separate this code and data required to draw the tree on the screen from the other data in the node that will be used for simplification.

I hope the question made sense. Thanks for the help and time.

+1  A: 

One approach is to create a 1:1 class hierarchy for views representing your tree nodes. That is, each tree node class has its own UIView subclass counterpart which knows how to draw the concrete tree node class. UIViews (and CALayers) already have all the necessary interfaces to maintain sizes, positions and even hierarchies. A possible downside of this approach is if you have a huge tree node class hierarchy (very many different classes of nodes), you will have as huge a number of view classes, which may become a little boring to code and support. Try to extract the most of common drawing functionality into a tree node view superclass and see if that helps simplify the node view class hierarchy by reusing view classes for different tree nodes, (of course, if it makes sense at all).

Another approach is to add some layout/drawing methods to model classes in the form of a protocol. Thus, your tree nodes know how to draw themselves, a single tree node view class calls those drawing methods and maintains sizes/positions.

In both cases a node view class has an instance variable referencing a node model and can access the node properties necessary to draw/layout the node.

Costique
A: 

It's not so clear what you mean as "maintain the position and size".

If you mean that given a node N that belongs to a Tree (T), it has the same parent and same children, you will clarify us if the transformation you apply to the T to get the simplified one (T1) outcomes a different tree or an homomorphic tree.

I don't know very well Objective C, but the question you ask us it seems general for every OOP language.

Given N the node you use in the tree T, you can declare an Inner Class "NData" inside the base class of N. So every node derived class will have the NData data to use in the simplification process.

I hope I understood the question :-)

robob
In order to draw the node, we need to know about its position, height and width which is calculated dynamically. Tree T will be simplified to a new tree T1. So basically, in the node class we need to maintain 2 types of data. Data that will be used to represent the node on the screen and the data that will be used to transform tree T to T1. My question is can both these data be in the same class (Node) or is there a better design to separate them?
Mithin
under a design point of view, I think it is better to separate them. If "Node" is the class name, I'll put an Internal class called "NodeData" inside Node class. So you effectively separate the data and link "NodeData" to "Node" class. If I remember it should be the "composition pattern" in the UML design language.
robob
I hope the comment was useful for you...
robob