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.
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);
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With