• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton members  RSS feed

 
Terry Moe
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

What is best practise when it comes to members of a singleton?
Should they be static or not? Why?

I guess that static members are a bit more flexible because they can be accessed both through the instance and the class. But are there any other concerns?

br,
TM
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea of the Singleton is to have a unique instance of a class. According to the GoF pattern this is typically achieved by means of an static variable that will hold a reference to the only instance of the class. The default constructor will be set to private to avoid unwanted class instantiation.

Then you provide a static getInstance() method that will check if the static variable has been properly initialized, if not a new instance of the class will be created, otherwise, the previously created instance of the class will be returned.

Hence, I would say that it is more logical that a Singlenton class have instance methods instead of static methods. Because the instance methods will be accessed by that sole instance that you obtained by means of the getInstance() method.

Now, what you seem to suggest is a class with just static methods and a private constructor. I am not sure if that falls into the category of a Singlenton, because you could no have an instance of the class. But in this case it sounds logical to me that you provide just static methods.

At least that's what I think.

What do you guys think?
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Edwin Dalorzo:
Then you provide a static getInstance() method that will check if the static variable has been properly initialized, if not a new instance of the class will be created, otherwise, the previously created instance of the class will be returned.


That method better be synchronized and by synchronized I mean properly synchronized not the broken double-checked locking. Better to use an initialization on demand holder or just add an initializer to the declaration and let lazy loading put it off until someone uses the class.
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
they are standard member fields, nothing static. the only static method would be the getInstance() method and the only static field would be your "instance" (ok, perhaps you need some more fields that are static, depends on your goal but usualy non-static)

and don't forget: don't use singletons unless you have a realy good reason....

there are a lot's of threads here about this topic so i wont go into the details.


pascal
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A class with all static methods and variables is a rough equivalent of a Singleton in many ways. But the "all static" class has even more challenges than Singleton. Among other things it's impossible to subclass and difficult to replace with another compatible class. Use either approach only after looking for other solutions to your requirements. Ask here if none come to mind.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!