The moose likes Beginning Java and the fly likes Example of delegating responsibility ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Example of delegating responsibility ?" Watch "Example of delegating responsibility ?" New topic

Example of delegating responsibility ?

Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
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

Joined: May 29, 2009
Posts: 122
not quite sure what you mean. maybe you want the observer pattern?
Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
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

Joined: Mar 13, 2009
Posts: 492

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.



"If the facts don't fit the theory, get new facts" --Albert Einstein
I agree. Here's the link:
subject: Example of delegating responsibility ?
It's not a secret anymore!