aspose file tools*
The moose likes Java in General and the fly likes Pros and Cons of getInstance() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Pros and Cons of getInstance()" Watch "Pros and Cons of getInstance()" New topic
Author

Pros and Cons of getInstance()

Tom Diamond
Ranch Hand

Joined: May 10, 2001
Posts: 98
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

Joined: Sep 10, 2002
Posts: 13
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

Joined: Jul 11, 2001
Posts: 14112
An interesting, mostly critical discussion is found at http://c2.com/cgi/wiki?SingletonPattern


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61224
    
  66

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


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
a la The Factory Method


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
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.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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

Joined: May 10, 2001
Posts: 98
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

Joined: May 05, 2000
Posts: 13974
I don't see that it would be significantly slower than using a normal constructor.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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
 
subject: Pros and Cons of getInstance()