Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Observer Design Pattern vs "Listeners"

People also ask

What is the difference between Observer and listener?

"A listener is essentially waiting for an event to occur on a given object, which is what an observer does" not exactly, according to the DP book observers don't wait for any kind of event, but a change in the state of the subject, which then the observer can query for (pull).

Is event listener an observer pattern?

In Listener Design Pattern, when a event happens, the event raiser notifies a global mediator (the singleton Events. instance ), because it has no information about the event handlers. While in Observer Pattern, when any change happens, the observed subject directly notifies all the observers.

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.

When should observer pattern be used?

Observer Pattern is one of the behavioral design 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.


Whether the term "listener" refers to the Observer pattern or not will depend upon the context. For example, Java Swing's "Event Listeners" are part of an Observer pattern implementation while .Net "Trace Listeners" are not.

It isn't uncommon for framework authors to assign different names to components participating in a given pattern implementation, but the official pattern names are generally used when discussing the patterns themselves.

Concerning design, the implementation of a given pattern will often be influenced by the language and platform being used. As such, a particular implementation of the Observer pattern within a given framework (which may happen to use the term "listener" to describe the role of the ConcreteObserver) might differ slightly from that described in the Design Patterns book.


There's a two-way nature to the description of Observer in Design Patterns by Gamma et. al. (GoF).

In their description of Observer, one of the ConcreteObservers might signal a change to its Subject. The Subject, which holds a list of all ConcreteObservers, then notifies its list. All ConcreteObservers, including the prime mover, then react as appropriate.

The common implementations of Listeners seem to all react to events from outside.

So, I would say that the Listener is a less-generalized case of an Observer.


A listener may well be an implementation of the observer pattern. A listener is essentially waiting for an event to occur on a given object, which is what an observer does.

I know you're not after a language specific answer, but it's kind of hard to talk about this stuff in the abstract. So if I were to investigate this in .NET, I'd be inclined to open an assembly containing a listener in .NET Reflector, which will allow me to disassemble the assembly and check its logic against a design pattern.