How is the Observer pattern different from the event driven model?

I’m a high-level developer, but I didn’t have much formal training, and although I used many design templates and saw how they were used as a developer in my years, no one went out of the way to say, “Oh, this is a template observer, or this is a Singleton pattern. "

Reading some design patterns, I came across an Observer pattern and it seems very similar to the way the .NET framework works. Did I miss something?

+13
events design-patterns observer-pattern
Apr 30 '09 at 16:20
source share
4 answers

The .NET event model is a fairly integrated implementation of the observer pattern in a common language runtime. .NET languages ​​implement observers directly in their language, using the built-in framework support.

In most programming languages, the observer pattern requires custom development or libraries.

It comes free as part of the language in C #, VB.NET, and most other languages ​​built to use the CLR.

+19
Apr 30 '09 at 16:23
source share

From MSDN

Those of you who are familiar with introducing types exposed in FCL will notice that IObserver, IObservable, or ObservableImpl are present in the Framework. The main reason for their absence is that the CLR makes them obsolete after the mod. Although you can certainly use these constructs in your .NET application, introducing delegates and events provides new and powerful tools for implementing the Observer pattern without developing specific types to support this pattern. In fact, since delegates and events are members of the first CLR class, the core of this template is included in the very core of the .NET Framework. Thus, FCL makes extensive use of the Observer pattern throughout its structure.

+6
Dec 12 '09 at 20:46
source share

Many event models, such as Java 1.1 and later, as well as the .NET event model, are basically implementations of the Observer pattern.

Note that this even applies to older mechanisms, such as using callback methods in C to handle events. This is one and the same intention, it is simply realized a little differently.

+4
Apr 30 '09 at 16:25
source share

Why do you think there should be a difference?

Don't you think that .NET developers also read design patterns?

Actually, the Observer pattern (like everything in the book) was well known long before they were classified and named Gof4. It was used to implement the .Net event model, as well as for the Win32 and Win16 event models, and possibly for many others.

+1
Apr 30 '09 at 16:22
source share



All Articles