18

I am going through Effective Java and some of my things which I consider as standard are not suggested by the book, for instance creation of object, I was under the impression that constructors are the best way of doing it and books says we should make use of static factory methods, I am not able to few some advantages and so disadvantages and so am asking this question, here are the benefits of using it.

Advantages:

  1. One advantage of static factory methods is that, unlike constructors, they have names.
  2. A second advantage of static factory methods is that, unlike constructors, they are not required to create a new object each time they’re invoked.
  3. A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their return type.
  4. A fourth advantage of static factory methods is that they reduce the verbosity of creating parameterized type instances.

Disadvantages:

  1. The main disadvantage of providing only static factory methods is that classes without public or protected constructors cannot be subclassed.
  2. A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.

Reference: Effective Java, Joshua Bloch, Edition 2, pg: 5-10

I am not able to understand the fourth advantage and the second disadvantage and would appreciate if someone can explain those points. I would also like to understand how to decide to use whether to go for Constructor or Static Factory Method for Object Creation.

Rachel
  • 100,387
  • 116
  • 269
  • 365
  • IMHO, it comes down to complexity: do you need to create more complexity if your code won't be extended by anyone but you? – Jonathan B Jan 06 '11 at 16:52
  • Even if no one else is going to be extending the code, it still needs to be easily understood if you come back to it in a few months time doing maintenance/bug fixing etc – Kurru Jan 06 '11 at 16:54
  • So, will it be fair enough to say that constructor will not create complexity and static factory methods will ? I am trying to understand which way will bring in more complexity ? – Rachel Jan 06 '11 at 16:56

3 Answers3

17
  • Advantage 4: When using a constructor, you have

    Foo<Map<Key, Value>> foo = new Foo<Map<Key, Value>>();
    

    vs

    Foo<Map<Key, Value>> foo = Foo.createFoo(); // no need to repeat
    

    this advantage will be gone in Java 7, when the diamond syntax will be introduced

  • Disadvantage 2. You can't easily tell whether a given static method is used for constructor, or for something else.

As for how to choose - there is no single recipe for that. You can weigh all of the above advantages and disadvantages given a use-case, but most often it will just be a decision driven by experience.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Bozho: For disadvantage 2, can you suggest a use case where not knowing whether a given static method is used for constructor or not will cause major issue ? I am just trying to understand what will the harm effects and why it is mentioned as disadvantage ? – Rachel Jan 06 '11 at 16:54
  • 3
    @Rachel: What would a static method `getFoo()` in class `Foo` do? Create and return a new `Foo`? Or return a child instance of `Foo`? Or even a singleton instance? Etc. Disadvantage 2 can however be overseen by choosing method names the smart way like `createFoo()`, `getChildFoo()` and `getInstance()`. It's all about writing self-documented code. – BalusC Jan 06 '11 at 16:57
  • @BalusC: Thank you for explanation, I was trying to compare Static Factory Method vs Constructor and see why is Static Factory Method recommended by the book. – Rachel Jan 06 '11 at 17:00
  • You mentioned something about `diamond syntax`, what is it and how it works ? – Rachel Jan 06 '11 at 17:05
  • 1
    `Foo> foo = new Foo<>();` – Bozho Jan 06 '11 at 17:45
6

If you know the concrete type of the class you are trying to create, then calling the Constructor is fine.

Static Factory Methods are nice when you're not entirely sure how to construct the object you need.

The Factory Method still calls the Constructor on the concrete type, but the method handles the decision on what kind of concrete class to instantiate. The Factory Method then returns an Interface type (rather than a concrete type) so that the concrete type is hidden from the caller.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • 1
    A useful aspect of factory methods is that in many cases it will be possible for future versions of a factory method to return additional types without the underlying code having to be altered. For example, an `ImmutableMatrixOfDouble.Create` method might see if its argument is a square matrix whose only non-zero elements are on the diagonal, and return an `ImmutableDiagonalMatrix`() in such a case. A 256x256 `ImmutableDiagonalMatrix` would only require about 1/256 as much space as a 256x256 array, so if many immutable arrays would happen to fit the pattern, one could save space. – supercat Jul 30 '12 at 18:05
0

Disadvantage 2.

A static method that is used for object creation has the same functional layout and appearance as any other static function.

Just by looking at a static method that creates objects you wouldn't know it does this, and the opposite is the relevant part. When you're writing code that you're unfamiliar with, it can be difficult to identify the correct static method that is used to create objects.

The use of the Static Factory Pattern is well documented and can be very useful, especially in Singleton and Multiton situations. Also useful in the initialisation of complex objects.

Kurru
  • 14,180
  • 18
  • 64
  • 84