JDK-6192554 : Need generic factory interface.
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.util
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-11-09
  • Updated: 2016-09-02
  • Resolved: 2016-09-02
One thing I've found to be extremely useful since the introduction of generics into my own APIs is the use of a common factory interface. For example:

public interface Factory<T>
    T create();

Since the introduction of generics, the API designer can use this interface to accept factories for the repetative or custom creation of typed objects. For example, the API of  a multi-map might be designed using a factory to create the collections that will be used to store multiple values under one key:

public interface MultiMap<K, V, C extends Collection<V>>
extends Map<K, C>
     * Sets the factory that will be used for creating the collections that will
     * store multiple values under each key.
    void setCollectionFactory(Factory<? extends C> factory);

     * Puts the specified value into the collection that is mapped to the
     * specified key and uses the defined factory to create a new collection
     * if the mapping does not exist.
    void putValue(K, V);

The use of the factory allows the developer to define the collection implementation that best fits his needs while the multi-map need not care what type of collection it is.

Factories are frequently used to hide implementation details and simplify code. I believe the frequency of use justifies the need to define a common interface.
###@###.### 2004-11-09 03:52:34 GMT

The interface java.util.function.Supplier<T> was added in Java 8, so this is no longer an issue. Closing as Not An Issue.

WORK AROUND Use java.util.concurrent.Callable<T>. But the names of the interface and of its single method (call()) are not very informative if in fact you are using it to mean a factory. ###@###.### 2004-11-29 10:09:46 GMT