|
|
Generic singletons
Is there a way to make a singleton out of a generic type? Considerthis 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 (sinceclearly one size fits all classes) other than to just "drop thegeneric info"?
|
|
0
|
|
|
|
Reply
|
kelvSYC822 (9)
|
4/4/2007 9:36:04 PM |
|
kelvSYC wrote:> 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"?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
|
|
0
|
|
|
|
Reply
|
Lew
|
4/4/2007 10:58:42 PM
|
|
On Apr 4, 2:36 pm, "kelvSYC" <kelv...@gmail.com> wrote:> 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"?Look at how java.util.Collections is emplemented (specificallyemptyList())
|
|
0
|
|
|
|
Reply
|
Daniel
|
4/5/2007 12:27:35 AM
|
|
|
2 Replies
98 Views
(page loaded in 0.04 seconds)
|
|
|
|
|
|
|
|
|