Skip navigation.
Home

C# delegates in Java

This is my attempt to implement the poweful C# delegate in Java.
I've searched the net trying to find an implementation to it in Java, but the result was furstrating.
some time it is too complex to grasp, like here
http://www.onjava.com/pub/a/onjava/2003/05/21/delegates.html?page=2
other implementation uses reflection to invoke the method


Beside, it seemd that delegate has a long debat history between sun and Miscorsoft, For some fun reading on this subject, check out the propaganda from both sides:



Anyway, I've made my own implementation of delegates in  Java, belwo is a comparsion with source code from C#  delagte and Java using my own delgat class.


I will begin by C#:


 


class Program


    {


        delegate void Delegate(String msg);


        static public void Job1(String msg)


        {


            Console.WriteLine("Delegate 1: Message is: {0}\n", msg);


        }


 


        static public void Job2(String msg)


        {


            Console.WriteLine("Delegate 2: Message is: {0}\n", msg);


        }


        static public void Job3(String msg)


        {


            Console.WriteLine("Delegate 3: Message is: {0}\n", msg);


        }    


        static void Main(string[] args)


        {


            Delegate myJobs = new Delegate(Job1);


            myJobs += Job2;


            myJobs += Job3;


            myJobs("Hello World");


        }


    }


Output:


Delegate 1: Message is: Hello World


Delegate 2: Message is: Hello World


Delegate 3: Message is: Hello World


 


Java Version:


public class SimpleDelegateTest


{


            public static void main(String ... args)


            {


                        Delegate del1 = new Delegate() {


                          public void Job(String msg)


                          {


                             System.out.printf("Delegate 1: Message is: %s%n", msg);


                           }


                        };


                       


                        Delegate del2 = new Delegate() {


                           public void Job(String msg)


                          {


                             System.out.printf("Delegate 2: Message is: %s%n", msg);


                           }


                        };


                       


                        Delegate del3 = new Delegate() {


                           public void Job(String msg)


                          {


                             System.out.printf("Delegate 3: Message is: %s%n", msg);


                           }


                        };                    


                        del1.add(del2.add(del3));


                        del1.executeJob("Hello World");


            }


}


Output:


Delegate 1: Message is: Hello World


Delegate 2: Message is: Hello World


Delegate 3: Message is: Hello World


 


Now, Here is the Delegate Calsss that I've made, it is very simple as you will see


 


abstract class Delegate


{


            Delegate nextDelegate;


            public Delegate add(Delegate delegate)


            {


                        if(nextDelegate == null)


                        {


                                    nextDelegate = delegate;


                        }


                        else


                        {


                                    nextDelegate.add(delegate);


                        }


                        return this;


            }


            abstract protected void Job(String msg);


            public void executeJob(String msg)


            {


                        Job(msg);


                        if(nextDelegate != null)


                        {


                                    nextDelegate.executeJob(msg);


                        }                     


            }


}



This is a very simple version of the calss (26 line of code), there is more complicated version that I keep, which has additional features, for example method is not restricted to String paramters onlym it can take any Kind and any number of paramters. also it prevents delegate dead locks and beside the add method, there is an implementation for the remove method.


Enjoy.


Re: Delegates in Java

I have written a reply to this... It's too long so I've made a post out of it [here | http://chaoticjava.com/?p=141].

Amr, http://chaoticjava.com/posts/delegates-in-java-why-not/ :) -------------%%% __Ahmed Hashim__%%%