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

我的梦境 提交于 2019-12-13 13:52:17

问题


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.


回答1:


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);



回答2:


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.




回答3:


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?



来源:https://stackoverflow.com/questions/38561736/joshua-bloch-item-1-consider-static-factory-methods-instead-of-constructors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!