The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Sending information to and from objects' contained objects

Hello, my application has two Panels on a Form.

-------
P1 | P2
-------

Basicly I need to update P2 based on stuff that happens on P1. But P1 isnt just a panel, since it contains another Panel P12.

One way to do this is

on P12 event =>
  Panel p1 = (Panel)P12.parent
  Form form = (Form)p1.parent
  form.updateP2 ();

This works (C# implementation). But it seams strange. What do you think?

PS: P1 and P2 are mostly there as placeholders created in the visual editor.

Thanks.
ExtremlyHackedApplications
Monday, August 06, 2007
 
 
The reason it seems strange is because P1, P2, and P12 are tightly coupled to each other. You might decouple them with the Observer pattern: http://en.wikipedia.org/wiki/Observer_pattern
dev1
Monday, August 06, 2007
 
 
If your components aren't reusable then I would think of them as a single unit so this is OK. It's straight forward and clear in the code. If you are making generic components than an events publishing system or an observer is a good choice.
son of parnas
Monday, August 06, 2007
 
 
Since you are already using events, why not have P2 add a handler to P1's change event, too?

P1 handles P12's Change event and then raises a P1 Change Event.
Cade Roux Send private email
Tuesday, August 07, 2007
 
 
I had a similar situation when trying to 'connect' two user controls (that were dynamically created in pairs).

I solved it by adding a middle-man (mediator) that has the logic to update the other control(s). My mediator talked to two Controller/Presenter classes for each UserControl but you can leave those out if there is no benefit of that abstraction.

Anyway, you create a Mediator class that hooks into the events of both controls (or Panels or whatever) and handles each event by updating the other control and visa versa.

If you dont use dynamic control creation you just initialize these Mediator classes after the InitializeComponent call, otherwise you have to hook them up at the same time the controls are dynamically created.

The nice thing about the solution is that you have all the coupling between UI elements in one place. Its easy to debug, maintain and change.
Marc Jacobi Send private email
Tuesday, August 07, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz