Deligation and forwarding

There is some confusion with using these terms. Usually word delegation is used when forwarding has to be used.

Lets consider the examle from http://forum.java.sun.com/thread.jspa?threadID=677566&tstart=40

> TrashMaster delegates the task of taking out the

> trash to an Employee

>

>publicclass TrashMaster{

Employee emp =new

> Employee();publicvoid takeOutTrash(){

> emp.takeOutTrash();}}publicclass Employee{

>publicvoid takeOutTrash(){ ...}}

I'll rewrite this with formattiong

publicclass TrashMaster{

Employee emp =new Employee();

publicvoid takeOutTrash(){

emp.takeOutTrash();

}

}

publicclass Employee{

publicvoid takeOutTrash(){ ...}

}

This is good example of forwarding, not delegation. Here job of taking out of trashis forwarded to Employee object.

Lets consider example of delegation

import java.sql.Timestamp;

import java.util.Calendar;

publicclass MyTimestampextends Timestamp{

private Calendat cal;

...

publicint getHours(){

cal.setTime(this);

return cal.get(Calendar.HOUR_OF_DAY);

}

}

Some explanation needed. Timestamp extends java.util.Date. setTime() of Calendar receivs java.util.Date as paramter. So, MyTimestamp is Timestamp, that is Date, that can be transfer to setTime() of Calendar.

Such transfering this object to other object in order to do real job isdelegation.

Forwarding can be seen as special case of deligation. Forwarding means forwarding calls to antoher object. Deligation means also deligation this to another object.

[3198 byte] By [alexsmaila] at [2007-10-2 5:34:14]
# 1

> Forwarding means forwarding calls to antoher object. Deligation means also deligation this to another object.

This is just tautology and doesn't explain anything. It doesn't explain what "forwarding" means, it doesn't explain what "delegation" means, and it doesn't explain why they mean different things.

Wikipedia says "Delegation is the handing of a task over to another person." The TrashMaster example does exactly that, it hands over a task to another object. But you say this should be called "forwarding". Then you provide another example that uses another object to help with some calculations. It doesn't hand over a task at all. And you say this should be called "delegation". Well, I disagree.

If we needed to calculate a predicted sales demand for an item in a warehouse based on its historical sales, and we needed to calculate some square roots as part of the calculation, you would say we were delegating to the Math class because we called Math.sqrt. I wouldn't call that delegation.

DrClapa at 2007-7-16 1:44:59 > top of Java-index,Other Topics,Patterns & OO Design...
# 2

> > Forwarding means forwarding calls to antoher

> object. Deligation means also deligation this to

> another object.

>

> This is just tautology and doesn't explain anything.

> It doesn't explain what "forwarding" means, it

> doesn't explain what "delegation" means, and it

> doesn't explain why they mean different things.

>

> Wikipedia says "Delegation is the handing of a task

> over to another person." The TrashMaster example does

> exactly that, it hands over a task to another object.

> But you say this should be called "forwarding". Then

> you provide another example that uses another object

> to help with some calculations. It doesn't hand over

> a task at all. And you say this should be called

> "delegation". Well, I disagree.

>

> If we needed to calculate a predicted sales demand

> for an item in a warehouse based on its historical

> sales, and we needed to calculate some square roots

> as part of the calculation, you would say we were

> delegating to the Math class because we called

> Math.sqrt. I wouldn't call that delegation.

From implementation points of view different between deligation and forwarding is, that with deligation reference to this object is transfered.

Math.sqrt recieves as argument primitive types, not object. It is impossible to deligate primitive type. But I can provide another example:

public class SqurableInteger {

private int num;

public SqurableInteger(int num){

checkSqurable(num);

this.num=num;

}

public static void checkSqurable(int num){

if(num<0) throw new IllegalParamerException();

int i=0, tmp=0;

while(true){

tmp=i*i;

if(tmp==num) return;

if(tmp>num) throw new IllegalParamerException();

i++;

}

}

public int getValue(){return num;}

public int getRoot(){return MyMath.sqrt(this);}

}

public class MyMath {

public static int sqrt(SqurableInteger sqInt){

if(sqInt==null) throw new IllegalArgumentException();

int i=0, tmp=0;

while(true){

tmp=i*i;

if(tmp==num) return i;

//never happened if(tmp>num)

i++;

}

}

}

In this code function getRoot() use delegation. It deligates itself as argument to the function MyMath.sqrt to do real job.

alexsmaila at 2007-7-16 1:44:59 > top of Java-index,Other Topics,Patterns & OO Design...
# 3

> with deligation reference to this object is transfered.

I thought you might have that in mind. I can't find anybody on the internet who thinks object-oriented delegation has anything to do with passing references to "this". So I think you just made that up. Can you point to a reference that agrees with you?

DrClapa at 2007-7-16 1:44:59 > top of Java-index,Other Topics,Patterns & OO Design...
# 4

> > with deligation reference to this object is

> transfered.

>

> I thought you might have that in mind. I can't find

> anybody on the internet who thinks object-oriented

> delegation has anything to do with passing references

> to "this". So I think you just made that up. Can you

> point to a reference that agrees with you?

I see you commenting on what's not delegation. Care to comment on what is delegation?

kablaira at 2007-7-16 1:44:59 > top of Java-index,Other Topics,Patterns & OO Design...
# 5
Basically I agree with the Wikipedia definition that I quoted in Reply 1. If an object is given a task (via a method call) and it gives that task, or some part of it, or some variant of it, to another object (via a method call), then that's delegation.
DrClapa at 2007-7-16 1:44:59 > top of Java-index,Other Topics,Patterns & OO Design...
# 6

In my last code im MyMath.sqrt line

int num=sqInt.getValue();

is missing.

> > with deligation reference to this object is

> transfered.

>

> I thought you might have that in mind. I can't find

> anybody on the internet who thinks object-oriented

> delegation has anything to do with passing references

> to "this". So I think you just made that up. Can you

> point to a reference that agrees with you?

I just mad myself clear, because my first attempt has failed.

From the conceptual point of view the diffrence is following. When you make forwarding to another object, than that object make calculation itself (or by doing deligation to another objects), that is without asking caller. When you making deligation to antoher object, than that object can ask caller to make some part of (or whole) of calculation.

alexsmaila at 2007-7-16 1:44:59 > top of Java-index,Other Topics,Patterns & OO Design...
# 7

Forwarding:

public void parse(File f) {

new FileParser().parse(f);

}

public void parse() {

new FileParser().parse(this);

}

Delegation:

public void parse(File f) {

FileParser aFileParser = new FileParser(f);

RecordHandler aHandler = new RecordHandler();

aFileParser.setHandler(aHandler);

aFileParser.parse();

while (aHandler.hasNext()) {

Record r = aHandler.next();

anObject.setName(r.getName());

// Something else useful.

}

}

Or another example:

public void startElement(String s) {

// do something

}

public void endElement(String s) {

// do something

}

public void parse(File f) {

FileParser parser = new FileParser(f);

parser.setHandler(this);

parser.parse();

}

The first is forwarding because the class is not providing any functionality of it's own, it's simply forwarding a call to another class that has the functionality.

The second examples are delegation because they delegate part of their task to other classes yet provides additional functionality. The class is delegating specific tasks beyond the scope of that class so that it can achieve it's task. For example, delegating the task of parsing the file to a FileParser and delegating the task of handling the FileParser's output to a RecordHandler. The FileParser does the job of actually reading the file, the RecordHandler does the job of parsing the data given the record format it's in.

Agree? Disagree?

kablaira at 2007-7-16 1:44:59 > top of Java-index,Other Topics,Patterns & OO Design...
# 8

Almost completely agree. Besides following:

> Forwarding:

>

> > public void parse() {

>new FileParser().parse(this);

> }

>

This is deligation example. Because in the parse() function could be call to another function of this. That is FileParser() can delegate part of his work to the caller.

Note also, that forwarding is special case of deligation.

alexsmaila at 2007-7-16 1:44:59 > top of Java-index,Other Topics,Patterns & OO Design...
# 9

SWLEADERSHIP, Thanks for correctnes. Delegation.

> > with deligation reference to this object is

> transfered.

>

> I thought you might have that in mind. I can't find

> anybody on the internet who thinks object-oriented

> delegation has anything to do with passing references

> to "this". So I think you just made that up. Can you

> point to a reference that agrees with you?

SWLEADERSHIP,DrClap/b], I found such refference at jawaworld

http://www.javaworld.com/javaworld/javaqa/2001-09/01-qa-0914-delegate.html

[quote] If the object decides to pass the request on, you say that the object has forwarded responsibility for handling the request to the second object.[/quote]

[quote]

Simple composition and request forwarding ... is often mistakenly referred to as delegation.

True delegation is a bit more rigorous. In true delegation, the object that forwards the request also passes itself as an argument to the delegate object, which actually does the work.

Think of true delegation this way: Something sends a request to object1. object1 then forwards the request and itself to object2 -- the delegate. object2 processes the request and does some work.

[/quote]

alexsmaila at 2007-7-16 1:44:59 > top of Java-index,Other Topics,Patterns & OO Design...