Listeners are an Observer implementation?

15

I'm trying to understand the Observer concept and its implementation, and I ended up finding this example where listeners is used instead of the observable and observer classes:

Thisgraphichasbeenremovedfrom , where an alternative way of implementing the same concept in Java is explained, however, using the EventObject and EventListener classes, of the java.util package.

Listeners are the basis of Swing / AWT, and by repairing the way graphic component actions are monitored, along with the above example, I noticed a certain similarity to the Observer pattern.

Would the implementation using Listeners (as in the above example and Swing) be a way of applying the Observer pattern?

Is there any difference in applicability between this form and with the use of Observable / Observer ?

Listeners increase coupling compared to Observable / Observer ?

    
asked by anonymous 27.04.2016 / 19:31

1 answer

10

Yes, it's the same. If you pay close attention the names are a bit different, but the function is the same. Observer or listener gives the same. Subject or font ...

There are several ways to do and get the same result, everything applies the default if you meet some requirements. That's why I say that default should not be followed as if it were cake recipe. It is, but everything deserves adaptation.

There is a larger coupling by the example shown. But I do not know if it is intentional for this case. Coupling may have been purposeful for this example. It is a way of requiring the observer to be written in a certain way to be able to sign that event. It is more common to leave a default method name for everything. Generally, notify() is chosen. But deep down the coupling is the least it has to be. I do not know if the name will affect so much, since the method has to know what to do with that specific event.

But this comes down to being an old Java bug. In languages that allow you to have functions passed as parameters (almost all languages) the method name does not matter, even an anonymous method is passed.

What the standard requires:

  • You can add and remove a method to a list of subscribers / listeners;
  • have a way to warn the whole list that a certain action has occurred;
  • the observer must have a method (subscriber / listener) able to receive the information about who is notifying him and what his current status is.

The "list" was on my own. As far as I know there is nothing that requires a list. Of course a list is much more useful, without it, it could only have one subscriber at a time and the others would fail to try to subscribe (at least I hope in a "good implementation", if you can call it that).

The rest is implementation detail. I understand that what's out there is very academic and confusing. The default is simpler than it looks.

See the diagram on Wikipedia . The code example is already complicated.

Totellyouthetruth,IfindthewayJavamakesthisveryconfusing.Irecommendusinga simpler way with Java 8 (this example is more complicated than should, but it's just what I found).

A document with an interesting insight into this pattern .

Just as an add-on, C # has event which makes it even easier to create the pattern. This mechanism coupled with delegate , which is the basis for the lambda event, was the reason C # exists. Sun did not accept that Microsoft put this in Java by creating the split for J ++, then J # and C # . >     

27.04.2016 / 20:14