My thoughts as an enterprise Java developer.

Wednesday, March 30, 2005

How may classes does it take to screw in a light bulb?

Is this factory overkill? First the factory class just caches an instance of the finder so the finder could just as easily have just static methods (there are no member variables in DocumentFinder). Second there seems to be no reason to declare finder as an IFinder and check instanceof since it must always be a DocumentFinder and even if it wasn't it would throw a NullPointerException. This looks like another case of mis-applied patterns. (BTW I think patterns are good when correctly applied.)

//FinderFactory just instantiates the class with Class.forName and stores
// the instance in a Map (it will return that instance on subsequent calls).

IFinder finder = FinderFactory.getFinder("com.app.finders.DocumentFinder");

DocumentFinder documentFinder = null;
if (finder instanceof DocumentFinder) {
documentFinder = (DocumentFinder)finder;
}
return documentFinder.findDocuments(documentSourceId);

No comments: