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?
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?
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.
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.
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.
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.