• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

anonymous inner class == OK OOD

 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My vague question is, "Is the use of anonymous inner classes considered to be decent object oriented design?"
I'm a bit surprised that my brief search didn't uncover much information on this topic. My tendency is to avoid anonymous inner classes because they aren't reusable. I do realize, that the class isn't intended to ever be used more than once and I can appreciate not coding or designing in anticipation of possible future project features, but it still seems to me that using anonymous inner classes needlessly inhibits the abilities of classes in the project.
I also tend to shy away from inner classes, just because they often aren't really necessary - a top level class could often be created to do what the inner class was doing.
These two constructs seem to me to be abused - used unnecessarily.
So, am I crazy for not liking anonymous inner classes? (Feel free to take shots at me for avoiding inner classes, as well.)
 
Ranch Hand
Posts: 775
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No comments to make on your sanity. If I think up something witty at your expense, I'll let you know.
I think part of the problem is in thinking of an anonymous class as, well, a class. Really all you are doing is creating an object that has such a minimal amount of change relative to its base class that it hardly warrants the effort to maintain an entirely separate piece of source for it.
Besides, when you have a lot of instances of anonymous inner classes with only slight differences (e.g. a slight change in one method to support a JUnit test, and dozens of such tests), coming up with names for the things can be downright irritating.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Better late than never...
Sure, there's plenty of room for abuse of anonymous inner classes. There's also lots of room to abuse regular classes. I don't think that we shouldn't use something just because there's a chance for abusing it. That would be like saying we shouldn't drive cars because some people DUI.
There may be other justifications for anonymous inner classes (I'll leave it to you to STFW ) but IMHO, the usefulness of anonymous classes comes not from reuseability which, as you correctly pointed out, it lacks. Rather, anonymous classes are useful because they provide for good encapsulation -- a more important and practically achievable goal of OO, again IMHO. If it makes sense to have a private, single-instance object take care of some small but specific task, then an anonymous inner class may be the quickest, if not most elegant, solution.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to agree with Reid and Junilu.
Additionally, I don't care much about the reusability problems of (anonymous) inner classes. After all, if I later feel the need to reuse (parts of) it, I am able to refactor to my needs. Good refactoring browsers even provide automatic refactorings like "anonymous to named inner class" (IntelliJ IDEA for example, feature request for Eclipse is posted ).
So I often use anonymous inner classes especially when developing test-first, following the mantra of "make it run, make it right, make it fast". Anonymous inner classes are often the easiest way to "get to green".
Additionally I use anonymous inner classes where you would use blocks in Smalltalk - sadly they are very verbose in comparison to blocks...
I also use AICs for simple State/Strategy implementations, along the lines of
 
whippersnapper
Posts: 1843
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a longish thread from the Usenet group comp.lang.java.programmer that touches on this topic.
OK, I haven't reread the thread. It's just something I remember coming across a long time ago.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic