Accessing parent elements in an object model efficiently

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?

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?

Answers


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.

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.


I'd suggest a tree structure, whereas each of your classes inherits from a tree node.

Example in 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!


Need Your Help

How can I add different types of posts in WordPress?

wordpress custom-post-type

It's a vague question and I've searched throughout the Google and come up short. I want to create a type of content called an Employee. He should have 2 pictures attached, some text, a Facebook pro...

Running code in javascript to make a PHP and HTML file run from an ONLOAD event

php javascript html javascript-events onload

As a Newbie I am trying to run a PHP file and then an HTML file automaticaly using an

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.