11

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.

divesh premdeep
  • 1,070
  • 3
  • 16
  • 28

3 Answers3

6

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.

betomontejo
  • 1,657
  • 14
  • 26
  • 1
    *[Class] does the job at hiding the parameterless constructor implementation of the type it represents.* I completely disagree, `newInstance()` hides nothing. To call the constructor directly, you need to know that a no-arg, visible constructor exists. To instantiate using `newInstance()`, you need to know that a no-arg, visible constructor exists. The behaviour of `newInstance()` is well specified, so it really *can't* encapsulate anything of consequence. And in neither case do you need to be aware of the actual *implementation* of the constructor itself. – Mark Peters Sep 05 '12 at 13:10
  • Mark, I agree with your statement that `Class.newInstance()` does not hide anything, i.e. the caller of this method must ensure that a no-arg constructor exists for the specified class instance. Applying the same line of thought to the factory pattern, if a client obtains a reference to a factory implementation (say, through injection) and invokes a method on it, shouldn't the client be sure that the injected factory instance does not simply implement all the factory interface methods to throw an `UnsupportedException`? In my opinion, rarely is anything *truly* transparent. – divesh premdeep Sep 06 '12 at 17:04
3

I don't think anything suggests that Abstract Factory is "a factory of factories". An AbstractFactory<T> does not create factories that create Ts, it creates Ts directly.

The idea that it's abstract is to allow the logic to create T to be injected. So for example, you could have:

public interface ConnectionFactory {
    Connection newConnection();
}

//passed to your object normally:
public class RealConnectionFactory implements ConnectionFactory {
    //...
}

//passed to your object when unit testing:
public class FakeConnectionFactory implements ConnectionFactory {
    //...
}

//...

public class MyDao {
   public MyDao(ConnectionFactory connectionFactory) {
       this.conn = connectionFactory.newConnection();
   }
}

In this case, ConnectionFactory creates Connections, 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, because Class is not abstract, and in fact it can't be extended. You can't ask for a Class<Integer> and have one implementation initialize the new value to 1, and another initialize the new value to 7.

You could however stretch things by saying something like Class<? extends InputStream> is an abstract factory of InputStreams, with concrete implementations Class<SocketInputStream> and Class<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.

Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • Well, technically you could call `Integer.class.getConstructor(int.class).newInstance(1);`, but it's hardly convenient. – Duncan Jones Sep 04 '12 at 20:23
  • 2
    @Duncan: You can, but that has nothing to do with abstract factory. The point of abstract factory is that the method you're calling is specified by the interface. This is the opposite: you have one concrete factory with multiple factory methods called in different ways. – Mark Peters Sep 04 '12 at 20:24
  • + 1 for the explanation and injection example. – divesh premdeep Sep 05 '12 at 08:25
1

In my opinion, he is referring to code such as the following:

Integer.class.newInstance();

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().

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254