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.
In my opinion, he is referring to code such as the following:
Where
Class<T>
is the abstract factory. It became concrete when you passed the type parameter, e.g.Integer
. You then called the "builder",newInstance()
.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:
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
orinterface
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 ofjava.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 thenewInstance()
method. It's probably clearer if you use it like:Class
plays the "Abstract Factory", andObject
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 ofClass
nor sub-Class
es. It returns instances of the type it represents. It neither is a "Factory Method" just as Bloch states in his book.I don't think anything suggests that Abstract Factory is "a factory of factories". An
AbstractFactory<T>
does not create factories that createT
s, it createsT
s directly.The idea that it's abstract is to allow the logic to create
T
to be injected. So for example, you could have:In this case,
ConnectionFactory
createsConnection
s, but is abstract because it is an interface.I tend to agree with you that
Class<?>.newInstance()
is not a canonical example of an abstract factory, becauseClass
is not abstract, and in fact it can't be extended. You can't ask for aClass<Integer>
and have one implementation initialize the new value to1
, and another initialize the new value to7
.You could however stretch things by saying something like
Class<? extends InputStream>
is an abstract factory ofInputStream
s, with concrete implementationsClass<SocketInputStream>
andClass<FileInputStream>
. This is not the traditional meaning of "abstract" though (there's still just one class:Class
).But even then it's useless as an abstract factory, because the way that you implement a new concrete version of the "factory" is by creating a new class that extends
InputStream
. This is hardly what abstract factories are intended for.