views:

296

answers:

3

I was initially going to implement an observer pattern in C# 3.0 to solve my problem although it wouldn't be implement exactly in the same way.

My problem is that I have a web application with users who can post messages. Notifications do not go directly to other users but to a distributed cache where a statistics objects are updated and users can check the statistics a decide if they want the updates or not.

I currently have a IObserver interface that would need to implement multiple Update() methods based on who is posting a message and how they do it.

I have also looked at the mediator pattern but I don't think it is a correct fit because instances of a mediator would not have a list of who is currently logged in.

I am now wondering if there is another established design pattern that would be more suitable or if I should just fuinish building out my current Observer pattern to fit my needs.

Thanks

+6  A: 

Can't you implement it via events/delegates? This is the standard way to implement the Observer pattern in C# and other .Net languages.

Oded
+1. This is the correct way to do it.
Brian
+3  A: 

Aren't .Net events just observer-patterns in disguise? :) You could have a class, say, Statistic, and have that class expose an OnUpdate() event.

cwap
Having to null-check your event's delegate before invoking it strikes me as being forced to look over your shoulder to ensure that someone is watching you. Which seems pretty weird conceptually.
frou
Agreed, always hated that as well :)
cwap
+1  A: 

C# has taken a design pattern and made it a first class citizen of the language. Why not simply use what is provided? I don't see anything in your example that cannot be done with the built in event structure in C#.

Ed Swangren
I'm not against using events, but I do want to keep from having any bidirectional dependency between a User and a Class that would implement the statistics update.
CountCet
There is no dependency. One class exposes an event, another handles it. The pattern reduces the need for coupling/dependencies between classes.
Ed Swangren