Joshua Bloch #Item 1: Consider static factory methods instead of constructors
Asked Answered
G

3

5

This is related to creating and destroying objects from the book 'Effective Java' by Joshua Bloch

Item 1: Consider static factory methods instead of constructors

This method translates a boolean primitive value into a Boolean object reference:

public static Boolean valueOf(boolean b) {
    return b ? Boolean.TRUE : Boolean.FALSE;
}

Note that a static factory method is not the same as the Factory Method pattern from Design Patterns [Gamma95, p. 107]. The static factory method described in this item has no direct equivalent in Design Patterns.

The author seems to be talking about the the difference between Static Factory Method and Factory Method Pattern. What exactly is the difference here?

Asa further matter, BalusC mentions in this thread, a link under Factory Method, java.util.Calendar#getInstance() which is a static factory method suggesting thereby that the static factory method is a subset of Factory Method Pattern.

Garrulous answered 25/7, 2016 at 7:16 Comment(3)
This May be helpful.#8147663Salon
Not really sure, but this instance of a static factory method does not return new instances. It "replaces" a primitive value with the corresponding Object that already has been created. So saying "it is not exactly a factory method" is kind of true, while it has great similarity. Or short: If you define a static factory method in such a way, that is has to return a new instance, then this does not meet the definition.Barbiturism
I see no question here.Nurse
H
10

The factory method pattern

A factory method is an interface for creating objects. A concrete implementation of this interface designates the concrete object to be created.

The factory method pattern is uses when a client must instantiate an object, but it should not know how it is created.

  +------------+       uses        +-------------+
  |   Client   |    ---------->    |   Factory   |
  +------------+                   +-------------+
        | uses                     | create()    |
        V                          +-------------+
  +------------+                          ^
  | SomeObject |                          |
  +------------+                          |
        ^                                 |
        |                                 |
+--------------------+   create  +------------------+
| SomeConcreteObject | <-------- | ConcreateFactory |
+--------------------+           +------------------+

The static factory method

The static factory method pattern is a way to write clean code. It is a way to give a constructor a more meaningful name to express what it does. E.g.

List<String> newList = new ArrayList<String>(otherList);

What does the code above mean?

Is newList a copy of the otherList or does the ArrayList keeps a reference of the otherList and just delegates calls to it (like a wrapper)?

I guess everyone knows what the code above does, because we read the javadoc. Nevertheless if static factory methods would have been used, the code whould be more clear without reading the javadoc. E.g.

List<String> copy = ArrayList.copyOf(otherList);
SortedSet<SomeObject> sortedSet = TreeSet.orderedBy(comparator);

With static factory methods it is also possible to write multiple 'constructors' with the same parameter list, because you can give everyone another name. This would not be possible using 'normal' constructors. E.g.

List<String> copy = ArrayList.copyOf(otherList);
List<String> delegateList = ArrayList.delegateOf(otherList);
Howerton answered 25/7, 2016 at 8:44 Comment(0)
D
1

Below is the definition of 'the Factory Method pattern from Design Patterns [Gamma95, p. 107]' "Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses."

The pattern is not a single static method, but an interface (or abstract) method that will be called, depending on the class that implements it.

Take a look at the following for examples: Factory method pattern


Since we are talking about design patterns, keep in mind that the above example contains bad practices (calling an abstract method from a constructor).

See: Is it OK to call abstract method from constructor in Java?

Donatelli answered 25/7, 2016 at 7:40 Comment(0)
R
1

The definition of the factory method pattern that Google pops up in the search results (from Wikipedia) is:

Definition. "Define an interface for creating an object, but let subclasses decide which class to instantiate. The Factory method lets a class defer instantiation it uses to subclasses.

The key thing here is that the factory is passed as an interface to the class which needs the created instances. This means that multiple implementations of the factory method are possible.

The static factory method uses a static method: the method is referred to statically, not via an interface. This means that there is only one implementation of the factory method possible.

Roundish answered 25/7, 2016 at 7:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.