开发者

Why .NET doesn't have built-in Observer pattern like Java? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago.

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?


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.


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.


You can achieve the same thing with events and spaghetti code and they will drive you crazy trying to cleanup after them as they lock themselves into your non-disposable memory blocks.

If you don't yet have .Net 4, use the well know observer pattern and type it out the old-fashioned way - by hand.

The observer pattern is not simply writing events longhand, it's the basis for re-usable and extendable code. It is also a more decoupled and elegant approach to your solution.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜