I have a following object model:
我有一个以下对象模型:
- Book -- Chapter 1 --- Page 1 ---- Image 1 ---- Image 2 ---- Text 1 --- Page 2 ...
Resources are way down at the page level. But, I need to know the full path to resources, from the resources' point of view.
资源在页面级别下降。但是,从资源的角度来看,我需要了解资源的完整路径。
One way, is to have resources be aware of their parents.
一种方法是让资源了解他们的父母。
So my Image object could have a "parentPage" property, which in turn could have a "parentChapter" property. This way, I could access the complete path via currentImage.parentPage.parentChapter. Is there a better way?
所以我的Image对象可以有一个“parentPage”属性,而该属性又可以有一个“parentChapter”属性。这样,我可以通过currentImage.parentPage.parentChapter访问完整路径。有没有更好的办法?
A couple of words on why I'd need to know the full path from a resource's point of view. I have an object model that gets walked and rendered on screen. The renderer descends from chapter level all the way down into the element/resource level (this is where the rendering occurs). However to display the resources, I need to know where they live (ie the actual path on disk) and this information is typically specified at the Chapter level.
关于为什么我需要从资源的角度知道完整路径的几句话。我有一个在屏幕上行走和渲染的对象模型。渲染器从章级别一直下降到元素/资源级别(这是渲染发生的位置)。但是,要显示资源,我需要知道它们的存在位置(即磁盘上的实际路径),并且通常在章级别指定此信息。
Thanks!
-- Edit -- Just to clarify, is this parent.parent approach the best? It forces child objects to know about the parents, which makes me uncomfortable. Coupling?
- 编辑 - 只是为了澄清,这个parent.parent方法是最好的吗?它迫使儿童对象了解父母,这让我感到不舒服。耦合?
4 个解决方案
#1
2
Whether or not you use Zachary's tree structure or you do it in a more type-specific way, the question about coupling lives on.
无论你是否使用Zachary的树结构,或者你是以更具特定类型的方式进行,关于生活的问题。
If there is a lot about an image that doesn't have anything to do with how the image is "hosted" in a page, you might want to use an intermediate type which has the context-dependent aspect and that contains an image instance (reference).
如果有很多关于图像与页面中“托管”图像无关的图像,您可能希望使用具有上下文相关方面且包含图像实例的中间类型(参考)。
Only you can decide if that is excessive, depending on the application and how important it is to reduce coupling and allow greater reuse of some of the constituents in other contexts.
只有您可以决定是否过多,具体取决于应用程序以及减少耦合的重要性,并允许在其他环境中更多地重用某些成分。
#2
6
I'd suggest a tree structure, whereas each of your classes inherits from a tree node.
我建议使用树结构,而每个类都继承自树节点。
Example in c#:
c#中的示例:
class TreeNode {
public TreeNode Parent { get; set; }
public List<TreeNode> Children { get; set; }
}
class Book : TreeNode {
... book attributes ...
}
... other classes ...
Actually, if you're worried about coupling the thing to ask yourself is it good coupling or bad coupling? If the coupling actually adds value and there is a logical reason to do it, do it. If not, it's wasted code. If you're using a language that supports generics, you can decouple it a little farther:
实际上,如果你担心耦合这个问题,那么问题就是好耦合还是坏耦合?如果耦合实际上增加了价值并且有合理的理由去做,那就去做吧。如果没有,那就浪费了代码。如果您使用支持泛型的语言,则可以将其分离得更远:
class TreeNode<TParent, TChild>
{
public TParent Parent { get; set; }
public List<TChild> Children { get; set; }
}
class Book : TreeNode<object, Chapter> { }
class Chapter : TreeNode<Book, Page> { }
class Page : TreeNode<Chapter, object> { }
Hope that helps!
希望有所帮助!
#3
0
Sounds like you want a doubly-linked list or possibly to even consider using a tree structure.
听起来你想要一个双向链表或甚至可能考虑使用树结构。
#4
0
You may be able serialize the object to xml and use linq to xml for parsing the information.
您可以将对象序列化为xml并使用linq to xml来解析信息。
If you must use instances of objects, another option may be to look at the composite pattern (see wikipedia)
如果必须使用对象的实例,则另一个选项可能是查看复合模式(请参阅*)
Eric
#1
2
Whether or not you use Zachary's tree structure or you do it in a more type-specific way, the question about coupling lives on.
无论你是否使用Zachary的树结构,或者你是以更具特定类型的方式进行,关于生活的问题。
If there is a lot about an image that doesn't have anything to do with how the image is "hosted" in a page, you might want to use an intermediate type which has the context-dependent aspect and that contains an image instance (reference).
如果有很多关于图像与页面中“托管”图像无关的图像,您可能希望使用具有上下文相关方面且包含图像实例的中间类型(参考)。
Only you can decide if that is excessive, depending on the application and how important it is to reduce coupling and allow greater reuse of some of the constituents in other contexts.
只有您可以决定是否过多,具体取决于应用程序以及减少耦合的重要性,并允许在其他环境中更多地重用某些成分。
#2
6
I'd suggest a tree structure, whereas each of your classes inherits from a tree node.
我建议使用树结构,而每个类都继承自树节点。
Example in c#:
c#中的示例:
class TreeNode {
public TreeNode Parent { get; set; }
public List<TreeNode> Children { get; set; }
}
class Book : TreeNode {
... book attributes ...
}
... other classes ...
Actually, if you're worried about coupling the thing to ask yourself is it good coupling or bad coupling? If the coupling actually adds value and there is a logical reason to do it, do it. If not, it's wasted code. If you're using a language that supports generics, you can decouple it a little farther:
实际上,如果你担心耦合这个问题,那么问题就是好耦合还是坏耦合?如果耦合实际上增加了价值并且有合理的理由去做,那就去做吧。如果没有,那就浪费了代码。如果您使用支持泛型的语言,则可以将其分离得更远:
class TreeNode<TParent, TChild>
{
public TParent Parent { get; set; }
public List<TChild> Children { get; set; }
}
class Book : TreeNode<object, Chapter> { }
class Chapter : TreeNode<Book, Page> { }
class Page : TreeNode<Chapter, object> { }
Hope that helps!
希望有所帮助!
#3
0
Sounds like you want a doubly-linked list or possibly to even consider using a tree structure.
听起来你想要一个双向链表或甚至可能考虑使用树结构。
#4
0
You may be able serialize the object to xml and use linq to xml for parsing the information.
您可以将对象序列化为xml并使用linq to xml来解析信息。
If you must use instances of objects, another option may be to look at the composite pattern (see wikipedia)
如果必须使用对象的实例,则另一个选项可能是查看复合模式(请参阅*)
Eric