In C++, I understand that it is possible to create a Singleton base class using templates. It is described in the book, Modern C++ Design, by Andrei Alexandrescu.
Can something like this be achieved in Java?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I have yet to see a satisfactory solution for this in Java. There are many examples using templates in C++ or generics in .NET, but Java’s generics implementation has some limitations with static members that make this difficult.
The best option I’ve seen is to use a static HashMap, and a factory class. This allows you to make a single factory that can serve multiple Singletons of any class.
For an implementation, see this post.