This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

How to communicate in java in a has a relation ship  RSS feed

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

other than implementing observer pattern, is there any other way to pass a message between a contained object to a container object?


Essentially, lets say i have a Computer class which contains a single SoundCard class. Essentially Computer has-a Soundcard.

now i Computer object can call methods on SoundCard objects, by passing a parameter -- essentially pass a message to the soundcard object.

but how does the other way happen? I dont want Soundcard to hold a reference to the computer object.

so how can soundcard pass a message to the Computer object.

one way i can think of is observer pattern. is there any other way?
 
Sheriff
Posts: 13394
221
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the Observer pattern, the Subject still needs to hold references to the Observers so they can be notified of any events they are interested in.  If you don't want the Soundcard to hold a reference to the Computer, you're still going to need them to be connected in some way. A message queue is one way to do that, where the Soundcard publishes to a topic that the Computer is watching. This is the Publish-Subscribe pattern.
 
Rancher
Posts: 3127
110
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or have a look at the PropertyChangeSupport class. Easy to use, see the API.
 
It's just a flesh wound! Or a tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!