A DESCRIPTION OF THE REQUEST : 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. JUSTIFICATION : 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
|