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

Anonymous Classes vs. Delegates

By , 8 Mar 2011
Rate this:
Please Sign up or sign in to vote.

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
No Biography provided

Comments and Discussions

 
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 
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.

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