• Post Reply Bookmark Topic Watch Topic
  • New Topic

Observer pattern - Unexpected output.  RSS feed

 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

I'm trying to create an Observable ListOfItems which will contain a list of Items. Each item will have two attributes - name and price. ListOfItems has an observer that is updated whenever there is a change in the observable- i.e whenever there is a new item added to or an existing item removed from the ListOfItems object. My code is as follows.









When I run the TestObservers class, I get the following output.

run:
Item{name=Cheese, price=10.0}
Item{name=Cheese, price=10.0}
BUILD SUCCESSFUL (total time: 0 seconds)

But I have added only one Item to my list. Why am I getting two Items? Shouldn't I get only one Item? Please advise.

Thanks,
Chan.

 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The display() method is being called twice. The lines of output come from that method, and there are two of them, so it logically follows that the method is being called twice. Does that help?
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes!

Sorry. I forgot to comment out the display method in the TestObservers class. Almost didn't see it.

Thanks.
Chan.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!