开发者

Iterate on a tree data structure and send information to an external class by considering the hierarchies of the tree

开发者 https://www.devze.com 2023-03-11 08:27 出处:网络
I have a class in the name of \'Tree\' that implements a tree data structure with non-homogeneous nodes (I have three kinds of nodes). According to principles of object-oriented programming, Just the

I have a class in the name of 'Tree' that implements a tree data structure with non-homogeneous nodes (I have three kinds of nodes). According to principles of object-oriented programming, Just the 'Tree' class has to have knowledge about details of handling and managing the tree and some operation such as add, search and etc.

In my GUI I want to add hierarchies of an instance of that 'Tree' class to a treeView component. Again according to the principles of object-oriented programming, the treeView has no information about the inner structure of tree and the inner structure of the tree is encapsulated and the 'Tree开发者_JS百科' class represents an abstract interface for the tree.

So the 'Tree' class can add nodes too treeView component but I don't prefer that the 'Tree' class have knowledge about presentation layer and knows how to add nodes to a special component like treeView.

The question is that I'm searching for a way such as using interfaces or delegates or something like these that the 'Tree' class iterates on the tree and sends the information for an external class but by considering the hierarchies of the tree structure.


I would use a combination of Composite to represent your Tree structure and a Visitor to visit that tree to build a UI TreeView. Here are a couple of good real world examples to get you started:

http://codebetter.com/jeremymiller/2007/10/31/be-not-afraid-of-the-visitor-the-big-bad-composite-or-their-little-friend-double-dispatch/

http://codeblitz.wordpress.com/2009/07/29/perfect-match-composite-and-visitor-pattern/

The purpose of the Visitor Pattern is to encapsulate an operation that you want to perform on the elements of a data structure. In this way, you can change the operation being performed on a structure without the need of changing the classes of the elements that you are operating on. Using a Visitor pattern allows you to decouple the classes for the data structure and the algorithms used upon them

0

精彩评论

暂无评论...
验证码 换一张
取 消