Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Observer Pattern and Event-Driven Approach

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?

like image 888
Carven Avatar asked Jun 22 '11 12:06

Carven


People also ask

Is observer pattern event-driven?

The Observer design pattern is modeled on the event-driven programming paradigm . Just like other design patterns, this pattern lets us define loosely coupled systems. Leveraging this pattern, we can write maintainable and modular software.

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 another name for the observer design pattern?

For an Observable object, we use the term Subject. Objects that are subscribed to the Subject's changes are called Observers. A Subject and Observers are typically in one-to-many dependency. The Observer Design Pattern is also known as the Event-Subscriber or the Listener pattern.

What is meant by observer pattern?

The observer pattern is a software design pattern in which an object, named 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.


1 Answers

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.

like image 155
Angel O'Sphere Avatar answered Sep 30 '22 12:09

Angel O'Sphere