• Post Reply Bookmark Topic Watch Topic
  • New Topic

Observer Pattern notification order.  RSS feed

 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I was trying out Observer Pattern and found one thing, when we used Observable and Observer java builtin Features. and in the code

observable.addObserver(observer1);
observable.addObserver(observer2);
observable.addObserver(observer3);

I found that notification is always taking place in reverse order of above list; that is it prints out updated info for object3,object2 and object1 respectively for every execution.

Does that mean to say that these Java-builtin APIs will always notify the observers in the reverse order only?

Thanks
Chandra
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the Javadocs for java.util.Observable (is that the one you're using?), they say:
The order in which notifications will be delivered is unspecified. The default implementation provided in the Observable class will notify Observers in the order in which they registered interest, but subclasses may change this order, use no guaranteed order, deliver notifications on separate threads, or may guarantee that their subclass follows this order, as they choose.
 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
m registering as observer1, observer2 and observer3 ; but the notification is taking in reverse order observer3, observer2 and observer1
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chandra shekar M wrote:m registering

Please UseRealWords: "I am".
but the notification is taking in reverse order observer3, observer2 and observer1

As said, you can't rely on any order.

I've checked the source code of java.util.Observable (from src.zip in the JDK folder), and this is in the notify method:
That's clearly reversed order. The Javadoc is partly incorrect; since Java 1.4 already the order is reversed. But then again, they do warn that you cannot rely on the order.
 
Chandra shekar M
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Javadoc is partly incorrect; since Java 1.4 already the order is reversed. But then again, they do warn that you cannot rely on the order.

Which is what the documentation is there for. The whole business of Object Orientation is to hide implementation details; so if you program based on something you discover by reading the source code you deserve everything you get. It's also quite possible that it could fall in a heap on a different system.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!