Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

singleton pattern in java

 
jayakumar elumalai
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have heard about using patterns in core java.Is it possible?If yes, how?
 
subhit chauhan
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
patterns are designing stuff.... you can use any types of patterns in any technology.... check this link out http://www.patterndepot.com/put/8/JavaPatterns.htm
for Java design patterns.....
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For example, a Java implementation of the Singleton Pattern might be as follows.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 http://javacodetips.blogspot.com/2014/04/design-patterns-singleton-pattern.html
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

and

Where Have All the Singletons Gone
 
Stephan van Hulst
Bartender
Pie
Posts: 5568
53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I loved your poem, John.

[edit]

It seems he posted it in 2003 -_____-;
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:I loved your poem, John.

[edit]

It seems he posted it in 2003 -_____-;


I didn't notice that either!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic