Function pointers in Java
I was writing some code this week where I passed in an interface which was used in the internal working of the code. I had seen this used once in the Collections framework where you pass in the comparator interface.
I will let Sun describe the Comparator Interface, here's a link if you wanna read it all http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Comparable.html
"This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.
Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or elements in a sorted set, without the need to specify a comparator."
Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or elements in a sorted set, without the need to specify a comparator."
here is the method summary
Basically it is used if you want to add a method to sort objects of the same type because the objects will be sorted using your compareTo code.
When you are using the comparator when sorting a collection you pass in the comparator and then when the Sorting code is run it does something like
Comparator.compareTo
so it uses the code you have written in the compareTo method with some other sorting code. What is really happening here is that you have passed a function into a class/method.
Why am I talking about his well it was because I didn't know what doing this was called but I have been using this and creating interfaces to pass into a class to change how the class acts. I didn't really know if this had a name but I refer to it as the Interface Framework Pattern. The reason I call it that is because the class you are passing the interface into then becomes a framework and the interface you pass in, fits into the framework of the class but alters what the code the framework runs.
Whilst wondering what this was called I read item 22 in my Effective Java book by Joshua Bloch and he has a section called substitutes for C Constructs (I haven't ever programmed in C) and item 22 is called "Replace function pointers with classes and interfaces" and he describes what I had been doing with my classes but in a much better way and with good grammar and no spelling mistakes.
He states that this type of code is basically using the Strategy method to swap strategies or a sorting pattern in the comparators case. Effective Java also recommends that you make the class a Singleton because you it doesn't hold any instance variables. I hadn't done this on mine, so I nipped back and made a few changes.
The examples of the code where for similar type of things. The first was reading in a text file and the second was for polling/watching a directory for a text file.
For the polling a directory example I used the FileAction Interface with the two methods, directoryChecking which returned a boolean and Action. The DirectoryWatcher class was the framework and it was passed in the FileAction interface assigned the value of my concrete class implementing the FileAction Interface. The DirectoryWatcher extended the TimerTask and every 30 minutes set off it's run() method. In the run method I took the Interface and did
FileAction.directoryChecking - this method checked for a certain file.
if the directoryChecking returned true then I would run the FileAction.Action()
Which in this case would process the file. If someone else wants to check a directory then they could right a concrete class that implements the FileAction interface and change the type of file or files to check for and then write a different action and not have to change the framework class.
What I really like about this coding style is that by taking the Interface as a parameter rather than implementing it the methods can be kept private and not accessible outside of the class. Initially I thought about using an abstract class to achieve a similar result but I would have had to make the methods protected so that they weren't visible outside of the package.
3 Comments:
If you're interested in exploring other design patterns like the Strategy pattern, I recommend Head First Design Patterns. Its "Head First" approach can approach the chessiness threshold sometimes, but it really is written in a visual, understandable way with a lot of actual (if contrived) Java code. It also explains what good OO principles each pattern exemplifies, and it even gives guidance on when to use and not use a given pattern. Just don't go overboard on breaking out design patterns, or you'll support the "Java programmers overengineer" stereotype.
By Art Vandalay, at Thu Jun 22, 06:03:00 pm 2006
I endorse Art's comment regarding Head First Design Patterns. It is very cheesy in approach but very clear about the patterns themselves. I found that I could only take the cheesiness in small quantities so I needed to dip into the book for short periods at a time.
By Gordon J Milne, at Thu Jun 29, 12:47:00 am 2006
Also check out Functors in the Apache Jakarta Commons project: http://jakarta.apache.org/commons/sandbox/functor/.
By Anonymous, at Thu Jun 29, 03:27:00 am 2006
Post a Comment
<< Home