I have a class and I would like to restrict its instantiation to only one Factory class, how can I prevent this object from instantiation by the new operator?
Share
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.
Make all the constructors package private; have a single Factory object in that same package that owns the public creation method for clients outside the package.
It’s easy to do with a single class, but I’m imagining that you’d like that Factory to be virtual: able to create several implementations of a single interface or abstract class.