• Post Reply Bookmark Topic Watch Topic
  • New Topic

Bypassing a singleton  RSS feed

 
Brian Agius
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on some enhancements of an application that contains some singleton classes. The problem is that I need to leave these singleton classes as they are and even need to instantiate them normally due to the new development.

What I would like to know is whether there is a better to implement this as I was going to create a constructor method in the singleton class that bypasses the getInstance() method.

 
Piyush Porwal
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, you can always change the behavior of getInstance by returning a new object each time it is called. It will keep your existing code work as it is (backward compatible).
Plus, if you want, you may change the constructor from private to public to create a new instance normally. But I have read that static methods are preferred over constructors to create a new object, so you may still want to avoid changing the modifier for the constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!