Difference between Observer Pattern and Event-Driven Approach

EventsDesign PatternsObserver Pattern

Events Problem Overview


I always found the Observer Pattern almost similar to the usual event-driven approach. Actually, I have almost believed that they are actually just different names referring to the same thing. They both use similar concepts to have something as a listener and even in the implementation, they are almost the same thing, that's to have a callback method/function to carry out an action. This is at least in Java.

In other languages say Actionscript/Flex, the events are more user-friendly and may look like it does more than just the observer pattern defines. But still, the concepts sound the same.

But is this really true? Is the Observer Pattern the same thing as the usual event-driven programming style?

Events Solutions


Solution 1 - Events

The Observer Pattern is a very special instance. Event-Driven can mean anything. In most Observer Pattern implementations the Observer is an object watching the observee. When the observee is changed, a method of the observer is called. Strictly speaking this is not an "Event". That means: various different actions on the observee, usually lead to the call of different methods in the observer. The semantics "what" got changed is in the method. In Event Driven Systems, you basically have one consuming object/method and the message what was changed or what happend is in the Event. That can be anything and is not limitd to the idea of observing something! That means: in an Event Driven System you get new semantics by adding new Event types. In an Observer Pattern you usually add semantics by adding a method to the Observer class. HOWEVER: no one is preventing you to implement an Observer as a special listern to ChangeEvents.

Solution 2 - Events

When there is a state change at the Publisher or Subject,

  • Event Driven Architecture (is a message-driven architecture), responsible to deliver message to Subscriber, asynchronously.

  • Observer Pattern (is a software design pattern), responsible to command Subscriber to do something, synchronously.

Solution 3 - Events

> Event-driven programming is a term to define a paradigm. whereas > Observable pattern is a design solution to make an application > event-driven.

Cheers!

Solution 4 - Events

The diffrence No.1 may be, that Event-Systems always have an eventdispatchthread which decouples observables from its observers so events may not reach the observers immediatly. While real observables call observer methods directly, event driven observables drop their events into an eventqueue. Then the EDT deliveres those events to registered listeners.

Solution 5 - Events

Synthesizing from multiple answers in this question, this hackernoon article, and my own experience, the key difference between the Observer Pattern and Event-Driven (Pub-Sub, say) Architecture is, in my mind, this:

In the Observer Pattern, the Observed maintains references to its Observers.

Whereas in Pub-Sub, the Broadcaster has no idea who its Listener(s) are. (Or even if anyone is there to listen.) The Listener may expect some data from the Broadcaster, but has no idea exactly where the event comes from. Maybe it comes from multiple classes or remote systems. Maybe not. It doesn't matter to either the Broadcaster or Listener.

Now, that's not to say these things are very different. Also, there are implementations that behave like either or both.

For example, the wisper rubygem allows you to act like either an Observer pattern or a Pub-Sub pattern depending on your need. You can even use both together if you like.

Solution 6 - Events

One of the great things about the observer pattern is safety. When code changes occur within the observee object, all observants must implement the new changes. If we would have used events instead, the event listener would not react to the implementation of new events. In other words, the observee dictates that events must be handled by the observers.

In one of my projects I came across a situation where one of the observee objects became destroyed and observers needed to be notified, before doing so. The only thing that I had to do was add the notification as a requirment for all of the observers, and fire the event before the observee got destroyed. The beauty here is that the code only compiles if all observers implement a handler for the newly created event, therefore you will less likely forget to change a few of them. In my case I had a decent amount of possible observers, and it is nice to know that I can make changes without creating a few or more hidden bugs.

These qualities makes the observer pattern less flexible than simply throw an event, and let the observers decide for themselfs. But, when using events, beware for the great spaghetti monster that hides around the corner, and only comes out when deadlines are near. There's probably more than a few of you that have, just like me, come across this ugly beast more than once.

In other words, be as pragmatic as possible and still use the right tools for the job. This means using event when you need flexibility and the observer when you need control.

Solution 7 - Events

I've searched a bit for this same question. For this thread, I find point from Angel O'Sphere on "What semantics" and point from Spacerat on "Dispatcher" do helps.

That two points are my understanding that distinguish Even-Driver from Observer Pattern. At least from canonical explanation, "Observer Pattern" usually represents an immediate broadcasting once the "Subject" has changed and the "Dispatching" is implemented by calling the interface provided by subscriber or listener. While for Event-Driven, there is always another layer between "Subject and "Observer". Either called "Dispatcher" or using "Event Queue". This provides the "delayed" handling to reduce CPU usage and also provides certain capability on calling different interfaces depends on different event type.

Solution 8 - Events

I try it very simple, because that helped me once too.

Just think as a Observer and Observable. Instead that the observable marks a setChanged and the observer requests from the observable what has changed, the observable instead broadcasts an object (Event Carried State) with all relevant information about the change to the observers. So there is actually one more instance between the Observer and the Observable.

http://www.grahambrooks.com/event-driven-architecture/patterns/stateful-event-pattern/

Solution 9 - Events

Yes, they are the mainly same.

Events are something like a "built-in" observer pattern template for some languages.

Thus, you wouldn't really implement the observer pattern in a language which supports events as they already provide what you are looking for.
On the other hand, you can write event-driven in languages which lack events by using the observer pattern.

Solution 10 - Events

The basic difference in both is coupling and synchronous behaviour. If we stick with observer pattern, we say there is only one source of signal and it would be synchronous, while on other hand with events, we decouple both parties to work independently and the same time entertain the possibility of having more than one source of events in future without any code changes. Events help us to embrace async as design. All Reactive programming library provide very good support for event driven design.

Solution 11 - Events

Event-driven is a software paradigm or style of writing code where objects communicate using events. Some languages like C# have native support for events. You can have an object raise an event to which another object listens to. The Observer Pattern is another way to achieve the same result.

Solution 12 - Events

From Wikipedia :

> 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, in > "event driven" software. Most modern languages such as Java and C# > have built in "event" constructs which implement the observer pattern > components, for easy programming and short code.

The observer pattern is a little more abstract and theoretical. Events are one (commonly built-in) implementation, however as noted in Angel's answer Events tend to be able to be used in a few other situations apart from what is strictly defined in the observer pattern.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCarvenView Question on Stackoverflow
Solution 1 - EventsAngel O'SphereView Answer on Stackoverflow
Solution 2 - EventsZeigeistView Answer on Stackoverflow
Solution 3 - EventsMohanView Answer on Stackoverflow
Solution 4 - EventsSpaceratView Answer on Stackoverflow
Solution 5 - EventsthekingoftruthView Answer on Stackoverflow
Solution 6 - EventsThomas van den BighelaarView Answer on Stackoverflow
Solution 7 - Eventsuser2189731View Answer on Stackoverflow
Solution 8 - EventsAnna KleinView Answer on Stackoverflow
Solution 9 - EventsMatthiasView Answer on Stackoverflow
Solution 10 - EventsRahul GargView Answer on Stackoverflow
Solution 11 - EventsMoshView Answer on Stackoverflow
Solution 12 - EventsiliketocodeView Answer on Stackoverflow