Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a situation where my parent knows about it's child (duh) but I want the child to be able to reference the parent. The reason for this is that I want the child to have the ability to designate itself as most important or least important when it feels like it. When the child does this, it moves it to the top or bottom of the parent's children.

In the past I've used a WeakReference property on the child to refer back tot he parent, but I feel that adds an annoying overhead, but maybe it's just the best way to do it.

Is this just a bad idea? How would you implement this ability differently?

Update 1: Adding more context. This is a rendering system so the parent container is a list of windows grouped together. The child item (window) that says "I'm most important!" wants to basically be rendered on the top of the rest of the windows.

The parent is just a logical container to group these children together. I can see where adding an event to signal the request to be on the top is a good idea. But implementation (what the child wants to do with the parent) aside, why wouldn't you want to have child->parent linking? Doubly linked lists do this so people can traverse to and from something.

share|improve this question
2  
You don't need a WeakReference. The .net garbage collector can handle cycles. If the child is no longer in use (parent not pointing to it), it will be collected despite containing a reference to the parent. –  dbkk Nov 22 '12 at 3:40
    
What happens if 2 children both think that they want to be the most important children? –  btilly Nov 22 '12 at 5:42
    
@btilly Most important child actually just reorders it to the top of the stack in the parent's child-list. So whoever does it last, becomes most important. In my scenario, you would never have conflict of most important. –  Thraka Nov 22 '12 at 5:57

3 Answers 3

up vote 16 down vote accepted

Is this just a bad idea?

Oftentimes.

  • It breaks encapsulation of the parent.
  • It increases coupling in both.
  • It serves as a breakout point for the child to get at the rest of the system, increasing coupling with anything vaguely near it (because people will abuse that reference)
  • It limits your design if you ever want children without parents.

How do do it better? The child should not know or care that it is in a collection. Instead of deeming itself important, it should signal that some event it knows about has happened so whomever cares (the parent) can increase its priority (or whatever is the rules for the context the child lives in). I'm not thrilled by that, and would perhaps prefer a better separation of concerns between the child's model and the importance behavior, but can't elaborate without more context.

[edit:]

Yeah, rendering systems are one case where parent ownership... well I don't want to say makes sense, but it's one case where it has been done and isn't the end of the world. For giving a control focus, I would still prefer the design where the input handler (or whatever) walks the tree and knows which collection to reorder rather than it finding the child, calling something on it which knows to go to its parent.

share|improve this answer
1  
This is a good answer and you should probably consider all the points raised by it and see if they apply to your problem. Having said that though I don't think it is the end of the world if you start with the reference as a simple implementation and refactor it out when/if things get out of hand. –  rperetti Nov 22 '12 at 3:36
    
The answer is right. But if child to parent linking is a bad idea, then object oriented programming is no longer related to real life objects. Isn't there any way to make this a good thing? –  Manoj R Nov 22 '12 at 5:41
    
Thanks for this answer so far. I've added more context in my original question. –  Thraka Nov 22 '12 at 6:05
    
@ManojR - object oriented programming was never related to real life objects. –  Telastyn Nov 22 '12 at 14:14
    
Your edit makes me think about the VisualTreeHelper in WPF\Silverlight. This allowed you to query about the relationship of the current control was to the rest of the UI systems controls. I guess I could implement something like this because I also have a root control that will host everything else. Thanks!! –  Thraka Nov 22 '12 at 17:28

How execution got to the point where child decides it want to be most important? Did it get there through parent? If yes, you could send reference to parent to that method.

eg. if all nodes have some kind of update() method that does something like

void update() {
    doSomething()
    for(Node n:childs){
        //do something
        n.update();
    }
}

you could change it to

void update(Node parent) {
    doSomething(parent)
    for(Node n:childs){
        //do something
        n.update(this);
    }
}
share|improve this answer
    
Yes this is a great way to do it. However, in my situation it could be possible that client logic code not initiated by the parent's loop. –  Thraka Nov 22 '12 at 17:15

I don't think it's a bad idea. You could solve this by adding a sort order value to each child. I'm envisioning something like "z-index" used to display objects ontop-of or behind each other in web pages.

I'm not sure how you would code something like this up, but the concept sounds feasible.

share|improve this answer
    
With this solution, the problem still exists though. This only replaces the order-in-the-array concept with z-index. I would still have to have a communication system from child-to-parent. Thanks though :) –  Thraka Nov 22 '12 at 17:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.