• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Observer pattern - Unexpected output.

 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If somebody says you look familiar, tell them you are in porn. Or in these tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic