Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Example of delegating responsibility ?

 
Michael Boehm
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an instance target being a member in an instance (owned by) o of class O. An instance u of class .U. should be able to express desire to make o change the state of target but not directly, so through an instance ui of class UI. Can someone give me an example of some Java class having code doing this in a nice way or maybe name the pattern I am close to here ? Might be time to get a book on design patterns
 
Mark Wa
Ranch Hand
Posts: 122
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not quite sure what you mean. maybe you want the observer pattern?
 
Michael Boehm
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets say we have a MediaCenter class. You can hook up all your elctronic gadgets to a MediaCenter. Now maybe there is an interface MediaController which provides the methods necessary for controlling instances of DvdPlayer, Projector instances etc. through the MediaCenter. Now you could have different classes MyPhone, Remote and similarly implementing MediaController to make them able to message the MediaCenter about increasing the volume on speakers, turn off the TV etc.
So the point where I dont feel I have a nice solution is how to "get a message" through from eg. a Remote to the MediaCenter endning in eg. the speakers.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're having trouble passing information between your "gadgets" I would suggest applying a new interface that will ease all of that communication. The Facade pattern is a pretty good one. If you just want your classes to be able to notify each other without going through a medium, I would use the Observer pattern like Mark suggested.

Facade
Observer

Hunter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic