Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In good OOP principle, should a nodeView ask to be added to a tree, or should a tree add the nodeView?

I wonder what is a good OOP principle, if in an iOS app, there is a UITreeView, and a UINodeView, with the UITreeView object having a rootNodeView, and this root node branches off with leftChildNodeView, and rightChildNodeView.

If each UINodeView object can be "dragged and dropped" any where in the screen, which is implemented in UINodeView's -touchesMoved handler -- is that good OOP principle? In addition, if a new nodeView foo is really close to one of the nodes that has no left or right child, the node foo can be added to that node as a child.

And I suppose if another nodeView that is bar and also has no parents (that is, also dangling), it makes sense that foo can be added as bar's child as well.

Should this foo nodeView "ask for permission from a node to be added as a left or right child" and "add it if allowed", or should the UIViewController or UITreeView detects that a node is moving inside itself, and "decide that it is close to another node (of all the nodes on screen) and has no left or right child, and add foo as a child"?

Obviously if only a node in the tree can add a child node, then the UITreeView can do the job, but if any node (dangling or not) can be a parent, then UIViewController or the main view UIView seems to need to do the job.

Does doing it one way or the other violate good OOP principles?

like image 904
Jeremy L Avatar asked Nov 13 '22 23:11

Jeremy L


1 Answers

I´d say the UITreeView should handle this and the Nodes should inform him via protocol/delegate-communication about position changes. The TreeView is the only object that can check, if a position change of a node results in collisioning with another node etc.

If you want to write real good OOP-code, try using a "Model-View-Controller"-pattern, where your View is your TreeView, your Model should hold all Node-Data-Objects and provide some methods for collision detection between the nodes, and your Controller should receive the position changes from your view, talk to the model, decide what to do and then take the apprioate actions (like adding a node as a leaf of another node).

That way you´re fully flexible for future changes, like using a database instead of RAM for data storage, or using the code for iPad instead of iPhone just by replacing the View.

like image 94
stk Avatar answered Jan 04 '23 03:01

stk