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 # . >