• 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
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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

 
Ranch Hand
Posts: 97
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: 12571
206
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.
 
Master Rancher
Posts: 2930
102
  • 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 is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!