Is there a way to make a singleton out of a generic type? Consider
this as an example:
public interface Delegate<Ret, T> {
public Ret transform(T t);
}
public class IdentityDelegate<T> implements Delegate<T, T> {
public T transform(T t) { return t; }
}
Is there a way to make a singleton out of IdentityDelegate (since
clearly one size fits all classes) other than to just "drop the
generic info"?
Lew - 04 Apr 2007 23:58 GMT
> Is there a way to make a singleton out of a generic type? Consider
> this as an example:
[quoted text clipped - 10 lines]
> clearly one size fits all classes) other than to just "drop the
> generic info"?
You can make a singleton out of any class by instantiating it only once in
your application.
Do you consdier, say, an IdentityDelegate<String> object and an
IdentityDelegate<Integer> object to be different instance of the same
"singleton" or two different singletons? In the JVM they will not differ in
type because of erasure.
-- Lew
Daniel Pitts - 05 Apr 2007 01:27 GMT
> Is there a way to make a singleton out of a generic type? Consider
> this as an example:
[quoted text clipped - 12 lines]
> clearly one size fits all classes) other than to just "drop the
> generic info"?
Look at how java.util.Collections is emplemented (specifically
emptyList())