I'm building a gui component that has a tree-based data model (e.g. folder structure in the file system). so the gui component basically has a collection of trees, which are just Node objects that have a key, reference to a piece of the gui component (so you can assign values to the Node object and it in turn updates the gui), and a collection of Node children.
我正在构建一个具有基于树的数据模型的gui组件(例如文件系统中的文件夹结构)。所以gui组件基本上有一个树集合,它们只是具有键的Node对象,引用gui组件的一部分(因此你可以为Node对象分配值,然后又更新gui),以及Node子集。
one thing I'd like to do is be able to set "styles" that apply to each level of nodes (e.g. all top-level nodes are bold, all level-2 nodes are italic, etc). so I added this to the gui component object. to add nodes, you call AddChild on a Node object. I would like to apply the style here, since upon adding the node I know what level the node is.
我想做的一件事是能够设置适用于每个节点级别的“样式”(例如,所有*节点都是粗体,所有2级节点都是斜体等)。所以我把它添加到gui组件对象。要添加节点,请在Node对象上调用AddChild。我想在这里应用这种风格,因为在添加节点时我知道节点是什么级别的。
problem is, the style info is only in the containing object (the gui object), so the Node doesn't know about it. I could add a "pointer" within each Node to the gui object, but that seems somehow wrong...or I could hide the Nodes and make the user only be able to add nodes through the gui object, e.g. gui.AddNode(Node new_node, Node parent), which seems inelegant.
问题是,样式信息仅在包含对象(gui对象)中,因此Node不知道它。我可以在每个节点中向gui对象添加一个“指针”,但这似乎有点错误......或者我可以隐藏节点并使用户只能通过gui对象添加节点,例如gui.AddNode(Node new_node,Node parent),看起来不那么优雅。
is there a nicer design for this that I'm missing, or are the couple of ways I mentioned not really that bad?
是否有一个更好的设计,我错过了,或者我提到的几种方式并不是那么糟糕?
3 个解决方案
#1
It seems to me that the only thing you need is a Level property on the nodes, and use that when rendering a Node through the GUI object.
在我看来,您唯一需要的是节点上的Level属性,并在通过GUI对象渲染节点时使用它。
But it matters whether your Tree elements are Presentation agnostic like XmlNode or GUI oriented like Windows.Forms.TreeNode. The latter has a TreeView property and there is nothing wrong with that.
但重要的是,你的Tree元素是否与XmlNode一样是表示不可知的,或者像Windows.Forms.TreeNode那样面向GUI。后者有一个TreeView属性,没有任何问题。
#2
Adding a ParentNode property to each node is "not really that bad". In fact, it's rather common. Apparently you didn't add that property because you didn't need it originally. Now you need it, so you have good reason to add it.
将ParentNode属性添加到每个节点“并不是那么糟糕”。事实上,它很常见。显然你没有添加该属性,因为你最初不需要它。现在你需要它,所以你有充分的理由添加它。
Alternates include:
- Writing a function to find the parent of a child, which is processor intensive.
- Adding a separate class of some sort which will cache parent-child relationships, which is a total waste of effort and memory.
编写函数来查找处理器密集型的子级的父级。
添加一个单独的类,它将缓存父子关系,这完全浪费了精力和内存。
Essentially, adding that one pointer into an existing class is a choice to use memory to cache the parent value instead of using processor time to find it. That appears to be a good choice in this situation.
实质上,将一个指针添加到现有类中是一种选择,可以使用内存来缓存父值,而不是使用处理器时间来查找它。在这种情况下,这似乎是一个不错的选择。
#3
I see no reason why you should not have a reference to the GUI object in the node. A node cannot exist outside the GUI object, and it is useful to be able to easily find the GUI object a node is contained in.
我没有理由为什么你不应该在节点中引用GUI对象。节点不能存在于GUI对象之外,并且能够容易地找到包含节点的GUI对象是有用的。
You may not want to tie the formatting to the level the node is at if your leaf nodes may be at different levels.
如果叶节点可能处于不同的级别,您可能不希望将格式绑定到节点所在的级别。
#1
It seems to me that the only thing you need is a Level property on the nodes, and use that when rendering a Node through the GUI object.
在我看来,您唯一需要的是节点上的Level属性,并在通过GUI对象渲染节点时使用它。
But it matters whether your Tree elements are Presentation agnostic like XmlNode or GUI oriented like Windows.Forms.TreeNode. The latter has a TreeView property and there is nothing wrong with that.
但重要的是,你的Tree元素是否与XmlNode一样是表示不可知的,或者像Windows.Forms.TreeNode那样面向GUI。后者有一个TreeView属性,没有任何问题。
#2
Adding a ParentNode property to each node is "not really that bad". In fact, it's rather common. Apparently you didn't add that property because you didn't need it originally. Now you need it, so you have good reason to add it.
将ParentNode属性添加到每个节点“并不是那么糟糕”。事实上,它很常见。显然你没有添加该属性,因为你最初不需要它。现在你需要它,所以你有充分的理由添加它。
Alternates include:
- Writing a function to find the parent of a child, which is processor intensive.
- Adding a separate class of some sort which will cache parent-child relationships, which is a total waste of effort and memory.
编写函数来查找处理器密集型的子级的父级。
添加一个单独的类,它将缓存父子关系,这完全浪费了精力和内存。
Essentially, adding that one pointer into an existing class is a choice to use memory to cache the parent value instead of using processor time to find it. That appears to be a good choice in this situation.
实质上,将一个指针添加到现有类中是一种选择,可以使用内存来缓存父值,而不是使用处理器时间来查找它。在这种情况下,这似乎是一个不错的选择。
#3
I see no reason why you should not have a reference to the GUI object in the node. A node cannot exist outside the GUI object, and it is useful to be able to easily find the GUI object a node is contained in.
我没有理由为什么你不应该在节点中引用GUI对象。节点不能存在于GUI对象之外,并且能够容易地找到包含节点的GUI对象是有用的。
You may not want to tie the formatting to the level the node is at if your leaf nodes may be at different levels.
如果叶节点可能处于不同的级别,您可能不希望将格式绑定到节点所在的级别。