This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Delegation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Delegation" Watch "Delegation" New topic


Richard Teston
Ranch Hand

Joined: Feb 12, 2002
Posts: 89
I understand that when an object recieves a request, the object can either handle it or delegate the request to another object. Does anyone has sample program for this, just a simple program will do. thanks.

The Code is the Programmer
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
Hi Richard.
You can look at the following article to see code example of delegation model.
If this doesn't make things clear then please do repost and I will try to write a longer reply with code example.

----------------------<br />regards.<br /><a href="" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="" target="_blank" rel="nofollow"></a>
Richard Teston
Ranch Hand

Joined: Feb 12, 2002
Posts: 89
I've read the article but i'm a little bit more confound with the example, sorry for asking but can you give me more simpler example or just explain its concepts and significants, i just like to see the pattern. Like when i asked, "how can i use model view controller in a simple gui which access the database" and someone answered...
"well model is for accesing the database and the view is for your graphical interface and the controller will the one who'll listen to the user's action and will glue together the view and model" and that's it. I put it into practice and then it worked. I'm studying design pattern and delegation is one that i'm focusing now and the decorator design pattern. I hope this was not to much to ask. Thanks for your first reply.
Karthik Guru
Ranch Hand

Joined: Mar 06, 2001
Posts: 1209
Consider a iterface that every utility provider needs to implement.

Say then you have another class , someone is using it already and it's does a good job of fethcing you grocery items.

Note that this class does not implement the Utility interface you defined earlier.
But it does the job! of getting you the grocery items
but does'nt confine to your interface and you expect everyone to stick to your interface.
So you have some options now.
1. Write a class Grocery that implements your Utility and copy the code from getGrocery method of GetGrocery class already available to you and make it work. c'd be messy.
2. Write a class Grocery that implements Utility and *delegate* the job to the GetGrocery instance

hope it's clear.
Richard Teston
Ranch Hand

Joined: Feb 12, 2002
Posts: 89
thanks karthik! it's a lot more clearer now, thanks for spending your time explaining this things.
I agree. Here's the link:
subject: Delegation
It's not a secret anymore!