Results 1 to 10 of 10

Thread: Anonymous classes

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Quote Originally Posted by SilverWarior View Post
    Why would you create a new class which incorporates only one method and such method is only used on one place? Wouldn't it be easier to just write your method nested within some other method where it is needed?
    Based on my understanding the purpose of classes is the ability to join a group of methods which serves similar purpose into one object so that you can easily reuse this object on different places without the need to redeclare all of those methods.
    Java Collections and Arrays has built-in support for sorting. You can either implement the Comparable interface or provide an Comparator object if you need to sort custom objects based on more than one property. This is simply how the Java RT API is designed.

    Note also that you may wish to implement more than one method. For Swing GUIs you may e.g. create a bunch of classes implementing listener interfaces handling various input states.
    The nice thing with anonymous classes is that an empty method will be created for all those methods you don't implement yourself.

    The thing is, that this is common practise in Java, and some Java libraries basically expect you to use anonymous classes. If you don't use anonymous classes you are gonna end up with 100's of small classes (for a larger project) with one or two functions in it designed for one specific case.
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  2. #2
    Quote Originally Posted by pstudio View Post
    The thing is, that this is common practise in Java, and some Java libraries basically expect you to use anonymous classes. If you don't use anonymous classes you are gonna end up with 100's of small classes (for a larger project) with one or two functions in it designed for one specific case.
    I didn't know that some libraries actually demmand annonymus classes in Java. But then again I do try to stay away from Java as I don't like its syntax. And now I also think I wouldn't like some approaches that are used in Java.

  3. #3
    Quote Originally Posted by SilverWarior View Post
    I didn't know that some libraries actually demmand annonymus classes in Java. But then again I do try to stay away from Java as I don't like its syntax. And now I also think I wouldn't like some approaches that are used in Java.
    Well they don't demand you to use anonymous classes. You can always get by with making traditional classes. It is just a **** of a lot more convenient to use anonymous classes in some cases.

    I wasn't a big fan of this design approach when I first met it but I suppose I've gotten used to it by now. If I was designing an API for Pascal compilers I certainly wouldn't require the user to create a bunch of classes implementing interfaces with one or two methods in it. But for Java this has become a design approach used by many. I guess it has something to do with the mantra that everything must be a class and in stead of letting people extend large classes just to implement one method they let them implement a small interface which they can then pass as an argument.

    Anyway there are many advantages using the Java RT imo, especially when I can use Oxygene in stead of Java, so this curious design pattern isn't going to stop me from using Oxygene. Not even if I have to create a bunch of small classes with only one method in them
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  4. #4
    Based on your description this sounds as a way of how Java tries to implement generics.
    So maybe you should look in this way to find your answer faster.

  5. #5
    Quote Originally Posted by SilverWarior View Post
    Based on your description this sounds as a way of how Java tries to implement generics.
    So maybe you should look in this way to find your answer faster.
    Maybe my description sounds like I'm talking about generics but that is certainly not the issue. Java and Oxygene both support generics and they are used where appropiate.
    The use of anonymous classes I'm describing is more a method of simulating delegates.

    Anyway as I write earlier, it turns out Oxygene has partial support for this feature. I'll have to do with that.
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

  6. #6
    For whomever it may concern the latest update to Oxygene for Java now supports Inline interfaces inheriting from classes. Clearly I was not the only one desiring this feature.
    Now I have to seriously think about if I want to renew my subscription.
    Imagine I've written something clever here inspiring you to make something awesome. If that happens give me credits

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •