Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is the Observer pattern different from an Event driven model?

I am a senior level developer but I haven't had a lot of formal training and I although I have used many design patterns and seen them used in my years as a developer, no one really went out of their way to say. "Oh this is an observer pattern, or this is a Singleton pattern."

Reading over some of the design patterns, I came across the Observer pattern and it seems to be to be very similar to the way the .NET framework events work. Am I missing something fundamental about this?

like image 266
Jeff Martin Avatar asked Apr 30 '09 16:04

Jeff Martin


People also ask

Is event an observer pattern?

The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. It is mainly used to implement distributed event handling systems.

What is the difference between the observer pattern and the pub/sub pattern?

In the observer pattern, the source of data itself (the Subject) knows who all are its observers. So, there is no intermediate broker between Subject and Observers. Whereas in pub-sub, the publishers and subscribers are loosely coupled, they are unaware of even the existence of each other.

What is the purpose of the observer pattern?

Observer design pattern is useful when you are interested in the state of an object and want to get notified whenever there is any change. In observer pattern, the object that watch on the state of another object are called Observer and the object that is being watched is called Subject.

What is the disadvantage of observer design pattern?

Disadvantages of Observer Design Pattern The main disadvantage of the observer design pattern that subscribers are notified in random order. There is also a memory leakage problem in the observer design pattern because of the observer's explicit register and unregistering.


2 Answers

The .NET Event model is pretty much a integrated implementation of the observer pattern in the common language runtime. The .NET languages implement observer directly in their language specific manner, using the framework's built-in support for this.

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

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

like image 90
Reed Copsey Avatar answered Oct 12 '22 21:10

Reed Copsey


From MSDN

Those of you with passing familiarity of the types exposed in the FCL will note that no IObserver, IObservable, or ObservableImpl types are present in the Framework. The primary reason for their absence is the fact that the CLR makes them obsolete after a fashion. Although you can certainly use these constructs in a .NET application, the introduction of delegates and events provides a new and powerful means of implementing the Observer pattern without developing specific types dedicated to support this pattern. In fact, as delegates and events are first class members of the CLR, the foundation of this pattern is incorporated into the very core of the .NET Framework. As such, the FCL makes extensive use of the Observer pattern throughout its structure.

like image 27
chikak Avatar answered Oct 12 '22 21:10

chikak