• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Pros and Cons of getInstance()

 
Tom Diamond
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I often see in apps that they use a private constructor and access the object with a static getInstance() method. So what are the pros and cons of this approach?
Is this a more "OO" method or not?
Tom.
 
Ashoke Bhowmick
Greenhorn
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom,
You are referring to the Singleton pattern.The private constructor is there not to allow any object of that class to be instantiated from outside.
The static getInstance() method is there to get a reference to the object of this class from outside..
There are tons of material available. Search the net for "Singleton Design Pattern".You will get your answer.
Regards,
Ashoke Bhowmick
Originally posted by Tom Diamond:
Hello,
I often see in apps that they use a private constructor and access the object with a static getInstance() method. So what are the pros and cons of this approach?
Is this a more "OO" method or not?
Tom.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interesting, mostly critical discussion is found at http://c2.com/cgi/wiki?SingletonPattern
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64205
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The utility of the getInstance() mechanism is not limited to just the Singleton pattern. It is useful whenever you want to have complete control over object creation; Singleton included. Examples: the Type-Safe Enum pattern, or a caching situation.
hth,
bear
 
Dirk Schreckmann
Sheriff
Posts: 7023
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In fact, I have seen it argued that getInstance should always be used and that no class should have public constructors! This gives complete control to the class author as they can provide a new instance with every call, pool instances, or provide a single instance. And they can change their mind and switch from one to the other without causing anyone who uses the class to have to change their code.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Paul:
In fact, I have seen it argued that getInstance should always be used and that no class should have public constructors! This gives complete control to the class author as they can provide a new instance with every call, pool instances, or provide a single instance. And they can change their mind and switch from one to the other without causing anyone who uses the class to have to change their code.

The problem with this is that with the increase in flexibility also comes an increase in complexity, so it seems to me...
 
Tom Diamond
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem with this is that with the increase in flexibility also comes an increase in complexity, so it seems to me...

OK with complexity (after all we can call it just another programming style) but what about performance it terms of speed and memory allocation? Is it OK to use such a style all the time?
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see that it would be significantly slower than using a normal constructor.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Paul:
I don't see that it would be significantly slower than using a normal constructor.

I am with Thomas here - the additional method call will be most probably insignificant compared to the cost of instanciation itself. In fact, the method call might even get inlined by the Hotspot Engine!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic