Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to communicate in java in a has a relation ship

 
Ranch Hand
Posts: 101
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 16597
278
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4633
182
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or have a look at the PropertyChangeSupport class. Easy to use, see the API.
 
reply
    Bookmark Topic Watch Topic
  • New Topic