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.

This seems to happen every time I create any sort of GUI. I have trouble figuring out how child classes should communicate to their siblings.

It's a general problem, but it's probably easier to use a concrete example (hopefully that's still okay for this forum? I know it's different from StackOverflow): I have a window that contains 4 panels. Right now, I simply have the children as member objects of the parent.

Keypresses change all 4 panels, so I have the child objects throw the event back to their parent, which does the logic, then makes the appropriate calls for each panel.

However, mouse events are different. They change the position of a camera (owned by the parent), which every panel uses to render itself. So I must recompute a few matrices, in order to render each panel. The matrices are different for each panel. The problem I'm having is: when do I give the children the matrices?

I think I have three possibilities:

  1. When reading a mouse event, compute the matrices and call setter methods for each panel. However, sometimes the matrix will be replaced before the window renders.
  2. Give the child panel a reference to the parent, and during the render call, request the matrix from the parent (either by method or member variable).
  3. Modify child.render() so that it accepts two matrix parameters, and make parent.render() call them appropriately.

#2 seems good, because I'm not unnecessarily setting the matrix in the child window. But it also requires me to mess with forward declarations for member objects, which seems like bad practice. And #3 I'm not quite sure how to implement, because render is called by many things other than my code (un-minimizing, for example), and I can't touch them. So I suppose it is #1?

Is there a design pattern people usually follow for this? Am I just overthinking, and #1 is actually completely OK?

share|improve this question
    
most gui framework I've worked with used #2 –  ratchet freak May 24 '13 at 21:53
    
Set a flag in the child in the setter, and then use an invalidation routine (what that looks like depends on what you are writing your GUI in) that will only render when it makes sense. So interim matrices are discarded. –  Amy Blankenship May 27 '13 at 13:05
    
What do you mean by an invalidation routine? –  Henry Swanson May 27 '13 at 14:10
add comment

1 Answer

up vote 1 down vote accepted

If I got your right, during the render call, the child needs a service which provides some matrixes, right? It does not really matter that it is the parent which provides that matrixes. So create an interface like IMatrixProvider, give a reference to an object of type IMatrixProvider during construction time to your panels and call that service when you need it within the rendermethod. Your parent object can implement that interface, if the matrixes are provided from there.

If you use a functional language or a language with functional elements, you may not even need a full "interface object", a simpler "call back" or "delegate function" may be enough.

This is mostly what you described under #2, but not with a reference to the full parent object - so the panels stay decoupled from the parent type.

share|improve this answer
    
Oh, that actually fixes two problems; I can just make the parent an ICameraProvider, so the code to compute the matrices is called less often, and in the right class. Thanks! –  Henry Swanson May 25 '13 at 18:10
add comment

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.