Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Generic singletons

Thread view: 
kelvSYC - 04 Apr 2007 22:36 GMT
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())


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.