Click here to Skip to main content
15,885,366 members
Articles / Programming Languages / Java

Anonymous Classes vs. Delegates

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
8 Mar 2011CPOL2 min read 16.4K   3   5
Sometimes, less syntactic noise is a good feeling for a programmer. I am not doing a language war here. I am just trying to vote for delegates in Java.

I am not a Java programmer. By that, I do not mean I am against Java. As a programmer by profession and passion, I try to learn things along the way. That includes a little of bit of Java. I should say that my proper encounter, so to say, with Java is a simple application that I am trying out with Android. There might be some hard core differences and/or limitations in the Android version of Java. But I am almost certain that I am using only the primary level features of Java.

In Android, there is this OnClickListener interface which is used as a callback interface for a button click. It is used something like this:

C#
// Create an anonymous implementation of OnClickListener
private OnClickListener mCorkyListener = new OnClickListener() {
    public void onClick(View v) {
        // do something when the button is clicked
    }
};

protected void onCreate(Bundle bundle) {
    ...

    Button button = (Button)findViewById(R.id.someButton);
    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        // Click handler action code...
        }
    });
    ...
}

OnClickListener, which is an interface with a single method onClick, represents a type for the button click event. The highlighted portion of the code that registers an event handler for the button click action is called an Anonymous Class definition. That is some really clever syntax, although it seems a wrong tool for our purpose here. Actually, the Click event requires only a method to call when the button is clicked. Nothing more. So why do we need an interface here?

I know of a better way in C#. Back there, it is called a delegate. In simple words, a delegate is an object-oriented pointer to a function, and it could point to any public\private instance\static function of any class. So a delegate is a good fit for our situation here. If the highlighted portion of the code (event registration) were to be written in C#:

C#
button.setOnClickListener(delegate(View v) {
    // Click handler action code....
});

I have gone one step further and used an anonymous delegate, which is even more succinct. Sometimes, less syntactic noise is a good feeling for a programmer. I am not doing a language war here. I am just trying to vote for delegates in Java. I am not sure if they are already there in one of the latest versions.

But there is a C# fanatic inside of me, which compels me to show the world how better and good-looking (see Pascal casing) C# code actually is.

C#
protected void OnCreate(Bundle bundle)
{
    var button = FindViewById<Button>(R.Id.SomeButton);
     button.Click += delegate(View v) {
        // Click handler code.
    };
}

Beauty lies in the eyes of the beholder!

Nevertheless, Anonymous Class is definitely a wonderful and powerful syntax, but does not look good in the example above.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhat about?... [modified] Pin
CZabransky22-Mar-11 3:48
CZabransky22-Mar-11 3:48 
AnswerRe: What about?... Pin
kb-boxer22-Mar-11 23:49
kb-boxer22-Mar-11 23:49 
GeneralRe: What about?... Pin
CZabransky23-Mar-11 3:13
CZabransky23-Mar-11 3:13 
GeneralWow Pin
kornman009-Mar-11 19:41
kornman009-Mar-11 19:41 
GeneralMy wish for delegates... Pin
supercat99-Mar-11 8:20
supercat99-Mar-11 8:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.