views:

221

answers:

5

I wonder why .NET framework doesn't have pair (Observer/Observable) interfaces similar to Java's feature?

EDIT: yes i know about events and delegates but using those interfaces is a simple and bookish approach of this DP isn't it?

+2  A: 

You can achieve the same sort of thing with Events. Also you can easily implement your own Observer pattern. It has to be one of the easiest patterns to implement: http://en.wikipedia.org/wiki/Observer_pattern

As for the why part. Not sure.

uriDium
+3  A: 

There is observable pattern via IObservable:

http://msdn.microsoft.com/en-us/library/ee817669.aspx

MUG4N
Not to be confused with Reactive Extensions (http://msdn.microsoft.com/en-us/devlabs/ee794896.aspx), see also the `system.reactive` tag here (http://stackoverflow.com/questions/tagged/system.reactive)
Benjol
The link you provide doesn't refer to a built-in .NET Framework interface, it's just an interface defined for the purpose of the article. Back in 2002 when that article was written, there was no IObservable interface in the BCL
Thomas Levesque
+6  A: 

Because .NET has actual events and delegates. Java lacks these basic constructs and has to resort to ugly hacks (your Observable interface) to pass "method pointers" around.

Blindy
ugly hacks? so why in NET4.0 they introduce Observable interfaces? (see next answer)
Arseny
@Arseny - they have similar names, but work quite differently.
Daniel Earwicker
This is a matter of interpretation, some languages do it one way, some the other. The statement is, IMHO, not very helpful ... and .Net does provide an IObservable to provide for this feature.
Obalix
This is bad to use bad words about good languages, created by good guys.
Odomontois
There are other uses for the observable pattern in C#, for example the Observable Collections in WPF. But for your question in particular, C# has a better alternative in its very syntax (and il-asm code).
Blindy
+11  A: 

In .NET 4 it does: System.IObservable<T> and System.IObserver<T> (which are a Dual of IEnumerable<T> and IEnumerator<T>). Look at the Reactive Extensions (Rx) project for compositional use of these interfaces with asynchronous events.

More generally, the Observer pattern is better served in .NET with events.

Richard
A: 

Where have you been. It is called events and delegates. Yes, it is a hack, but it works and more people prefer to use the language built in features over a design pattern you must type in yourself. The language feature is already debugged and ready to go.

Jim Lonero