Click here to Skip to main content
Click here to Skip to main content

Anonymous Classes vs. Delegates

By , 8 Mar 2011
 

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:

// 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#:

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.

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)

About the Author

Vivek Ragunathan
Software Developer (Senior)
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWhat about?... [modified] PinmemberCZabransky22 Mar '11 - 3:48 
AnswerRe: What about?... PinmemberVivek Ragunathan22 Mar '11 - 23:49 
GeneralRe: What about?... PinmemberCZabransky23 Mar '11 - 3:13 
I figured you would say as much. I can't argue, to say the least. I've always hated writing anonymous classes; they break the flow and elegance of my design. :|
GeneralWow Pinmemberkornman009 Mar '11 - 19:41 
GeneralMy wish for delegates... Pinmembersupercat99 Mar '11 - 8:20 

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 8 Mar 2011
Article Copyright 2011 by Vivek Ragunathan
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid