File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes singleton pattern in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "singleton pattern in java" Watch "singleton pattern in java" New topic

singleton pattern in java

jayakumar elumalai

Joined: Apr 21, 2003
Posts: 1
I have heard about using patterns in core java.Is it possible?If yes, how?
subhit chauhan
Ranch Hand

Joined: Jun 20, 2002
Posts: 40
patterns are designing stuff.... you can use any types of patterns in any technology.... check this link out
for Java design patterns.....
Dirk Schreckmann

Joined: Dec 10, 2001
Posts: 7023
For example, a Java implementation of the Singleton Pattern might be as follows.

[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
As you study singletons, be aware of a pattern once (but no longer) recommended by Sun that has been shown not to work on multi-processor machines and in some cases in multi-threaded apps on a single processor. If you see code like:

Run away! Here is an article on whyDouble-Checked Locking can get you in trouble.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937

The Singleton
Oh Singleton, -- to use or not to use?
The grace of pattern overwhelms me, --
You've been the subject of abuse,
Or so the sheriffs try to tell me
The heat of virtual hot spot machine
Makes my initializers slow and lazy
But the class loaders are really mean,
They make two instances, amazing!
And then the garbage man comes in
To claim the memory unreferenced
And when I call it from the bean
It's doubleton, as if I had no preference!
The call to get an instance comes along,
But gets preempted by someone to blame.
I now have a tripleton, and I am prone
To crashes, errors, tears, and pain.
Erkan Yalcin

Joined: Mar 30, 2014
Posts: 2
It's important for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager. A digital filter will have one A/D converter. An accounting system will be dedicated to serving one company.

You can find more and more at this adress
Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 685
I'm strongly of the opinion that Singletons are a bad idea. Note that 'Singleton' means more than 'only one instance'. Basically, they can make your code very hard to test. See these two blogs by a Google engineer:

Singletons are Pathological Liars


Where Have All the Singletons Gone
Stephan van Hulst

Joined: Sep 20, 2010
Posts: 4634

I loved your poem, John.


It seems he posted it in 2003 -_____-;

The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Mike. J. Thompson
Ranch Hand

Joined: Apr 17, 2014
Posts: 685
Stephan van Hulst wrote:I loved your poem, John.


It seems he posted it in 2003 -_____-;

I didn't notice that either!
I agree. Here's the link:
subject: singleton pattern in java
It's not a secret anymore!