Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Class.newInstance() follow the "Abstract factory" design pattern?

I have started reading Joshua Bloch's "Effective Java" (second edition). While reading item 2 (Consider a builder when faced with many constructor parameters), there is a particular statement that the author makes w.r.t the Class.newInstance() method. Specifically, the author says that

The traditional Abstract Factory implementation in Java has been the "Class" object, with the "newInstance" method playing the part of the "build" method.

This part has me confused a little bit - my understanding of the Abstract factory design pattern is that it is used to represent a factory of factories. The Class.newInstance() method, in my opinion, borders more on the "static factory method" coding philosophy (which incidentally, is item 1 in the same book)

Thoughts, anyone? I have been preparing hard to crack a few tough interviews and would really appreciate it if my fundamentals were solid before appearing for such interviews.

Thanks.

like image 379
divesh premdeep Avatar asked Sep 04 '12 19:09

divesh premdeep


1 Answers

Here's my opinion.

First of all, the Abstract Factory pattern is not intended to be a factory of factories. The key aspect of this pattern is that there is an accessible interface with an underlying (probably inaccessible) factory implementation through which you can get accessible interfaces of (probably inaccessible) object implementations. I know, is a long nasty wordplay of how I understand some of the applicability conditions of this pattern in Gamma's book:

  • a system should be independent of how its products are created, composed, and represented
  • you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.

At the end you're getting objects, not factories.

Second, I wouldn't make 1:1 relationships between pattern concepts and language keywords. "Abstract Factory" does not necessarily always translate to the Java abstract class or interface constructs. You can still have a regular, extendable, instantiable class that represents an "Abstract Factory" as long as you somehow guarantee that the client code is independent of the underlying factory and object implementations. This is the case of java.lang.Class, which is not abstract nor an interface, but does the job at hiding the parameterless constructor implementation of the type it represents through the newInstance() method. It's probably clearer if you use it like:

Object o = Class.forName(type).newInstance();

Class plays the "Abstract Factory", and Object plays the "Abstract Product" to the type implementation.

Last, newInstance() is not a static factory method, I think because this pattern is intended to return instances of the class it is implemented on. newInstance() does not return instances of Class nor sub-Classes. It returns instances of the type it represents. It neither is a "Factory Method" just as Bloch states in his book.

like image 54
betomontejo Avatar answered Oct 10 '22 23:10

betomontejo