Sunday, June 1, 2008

Delegation - Simple yet powerful concept

What is delegation and why is it powerful ?

Delegation is very simple to understand. Thats what all the team leads and track leads do every time, isn't it ? Don't work on your own but delegate it to the team members :)

In object oriented programming, delegation is used to describe a situation where one object defers task to another object, known as delegate.


class A {
public virtual void Foo() {
Console.Write("Object A doing the job.");
}
}


class B {
A obj = new A();

public virtual void Foo(){
obj.Foo();
}
}

Any object of class B will delegate execution of function Foo to object of class A.

But is that a big deal ? There would be many classes in my system and they would use each other.
1. So is every call made by an object to another object is delegation ?
2. And why is so powerful ?

For 1, I would say Yes. If an object calls a method of another object it is delegation.

For 2, lets rewrite the above example as

class A {
public virtual void Foo() {
Console.Write("Object A doing the job.");
}
}
class B{
A obj = null;
public B(A a){
obj = a;
}
public virtual void Foo(){
obj.Foo();
}
}
A obj = new A();
B obj2 = new B(obj);
obj.Foo();

Now, one can change the behavior of objects of class B without having to change the code of class B itself, by deriving new delegates from A and that makes it powerful. (Obviously it makes of dynamic binding in that case.)

One of the task that an object should always delegate is checking for equality of other objects.

class A {
private String name;
public A(String name){
this.name = name;
}
public String Name{
get{
return this.name;
}
}
public override bool Equals(object obj){
A obj1 = obj as A;

if (obj1 == null){
return false;
}
return String.Equals(obj1.name, this.name);
}
}


Lets assume that two instance of A would be considered equal if they have the same name, then one of the ways to check for equality is as below

A obj1 = new A("test");
A obj2 = new A("test1");
if (obj1.Name == obj2.Name){
}

But this is not a good idea as any change in class A might lead to a change in the class where this piece of code is written. The other better option is to delegate it to class A as

if (obj2.Equals(obj1)){
}


All said and done, delegation helps

1. Your application stay loosely coupled. That means your objects are independent of each other i.e. changes in one object don't require you to make changes to other objects. Delegation shields your objects from implementation changes to other objects in your software.

2. It makes your code reusable. It lets each object worry about its own task. This means your objects are more independent of each other i.e. loosely coupled. And looslely coupled objects can be taken from one application and easily be reused in other as they are not tightly tied to the other object's code.

Another question that comes to my mind...is delegation always achieved through composition ?

Also, when you would compile class A above, you would get a warning

'ConsoleApplication1.A' overrides Object.Equals(object o) but does not override Object.GetHashCode(). Leaving that for self study :)

No comments:

Post a Comment