Daniel Shevchenko

Can anyone suggest some good books on OOAD with UML and C#, design patterns and etc

Now to my question: I have a parent class A that holds a collection of classes B. Whenever values that classes B hold change I¡¯d like parent class A receive some sort of notification to lets say adjust its status to ¡°modified¡± to know whether it needs to save changes. Now what would be the best practices way of implementing this

¡¤ Make classes B only accessible through methods of class A, thus class A always ¡®knows¡¯ of changes made to classes B

¡¤ Make classes B accessible directly and call a method in class A from class B to notify it of changes whenever B changes

¡¤ Make classes B accessible directly and fire up an event whenever a change occurs

I mean any of these would work but maybe someone with more experience can suggest which solution would be most elegant and flexible.

Thanks in advance for your responses!

Daniel



Re: Visual C# General Notifications or Events between related classes?

BlueMikey

I view #1 and #3 as the most valid solutions.

It mostly comes down to how much sense it makes to have Bs exposed to the world. For example, I should be able to touch a volume knob on a radio, I shouldn't be able to touch a circuit inside. So it really comes down to whether or not your Bs are knobs or circuits.

And one advantage to using events is that Bs are easily used in other places still. If you want to be notified of B changes elsewhere, you don't have to write an explicit manager to handle them, anyone who chooses to know about changes can get that. But that's not important if your Bs don't need to be exposed, so that comes back to the first point.




Re: Visual C# General Notifications or Events between related classes?

Daniel Shevchenko

Imagine Bs being cells in the spreadsheet and As being row headers holding the sum of all Bs in a row. Whenever B changes i could recalculate the sum, or by handling an event adjust it to get to a new total. I may need to also implment class C that is a column header that holds total for each column, this way events seem like a good idea, but im a little bit concerned that following this pass i register two event handlers per B (i.e. each cell). How bad of an implementation is that



Re: Visual C# General Notifications or Events between related classes?

BlueMikey

Not at all. It even makes real-world sense.

The user is actually editing the cells. They aren't saying, "I would like to increase the sum of the row, so I will update the single cell over here." Instead they are saying, "I would like to update this cell and I expect that will cause an increase in the row sum."

So that word "cause" basically translates in our world to "event".

And certainly is valid to have two different collections reference a single object and both receive events when changes are made. When the 5 o'clock whistle blows, everyone hears it and quits working.