implementing Singleton using enums

P

puzzlecracker

Guys, I saw the following code that, allegedly, implements
Singleton's pattern via the enum... I don't fully understand it since
I've just got introduced to java's enums (pardon me, I am a C++
veteran with old-fashion understanding of the term). Please elaborate
what it means.


public enum SingletonUsingEnum
{
INSTANCE;
public void leaveTheBuilding()
{
System.out.println("A single instance");
}
}

thanks,

Puzzlecracker
 
J

Jeff Higgins

puzzlecracker said:
Guys, I saw the following code that, allegedly, implements
Singleton's pattern via the enum... I don't fully understand it since
I've just got introduced to java's enums (pardon me, I am a C++
veteran with old-fashion understanding of the term). Please elaborate
what it means.


public enum SingletonUsingEnum
{
INSTANCE;
public void leaveTheBuilding()
{
System.out.println("A single instance");
}
}

thanks,

Puzzlecracker

<http://en.wikipedia.org/wiki/Singleton_pattern#The_Enum-way>
<http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html>
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9>
 
J

John B. Matthews

puzzlecracker said:
Guys, I saw the following code that, allegedly, implements
Singleton's pattern via the enum... I don't fully understand it since
I've just got introduced to java's enums (pardon me, I am a C++
veteran with old-fashion understanding of the term). Please elaborate
what it means.


public enum SingletonUsingEnum
{
INSTANCE;
public void leaveTheBuilding()
{
System.out.println("A single instance");
}
}

Each enum constant identifies a single instance of the enum type, having
whatever members, constructors and methods are defined for it. In your
example, a single instance named INSTANCE with a single method. I found
these references helpful:

<http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html>
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.9>

Here's a familiar example using enums to encapsulate symbols and
functions:

<http://home.woh.rr.com/jbmatthews/java/calc.html>
 
R

Roedy Green

see http://mindprod.com/jgloss/enum.html

look at the "under the hood" decompilations to see how Java builds
little enum constant objects and collects them into a values[] array.
This is just a trick to tap into that logic. I singleton is usually
lazily constructed. An enum constants are constructed the first time
the class in used.
 
M

Mark Space

Jeff said:

This is a copy of the text from Effective Java, 2nd edition, and
probably explains the Java idom best.

If the OP can grab a copy of the 1st edition Effective Java book,
there's an item that explains how to implement the Java enum manually,
for language versions that don't have the enum keyword. It adds some
insight to why Java enums work the way they do -- as classes in addition
to enums.
 
L

Lew

Roedy said:
look at the "under the hood" decompilations to see how Java builds
little enum constant objects and collects them into a values[] array.
This is just a trick to tap into that logic. I singleton is usually
lazily constructed. An enum constants are constructed the first time
the class in used.

Lazy construction is not allocation of the singleton section /per se/.

--
Lew


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"The war on terror involves Saddam Hussein because of the
nature of Saddam Hussein, the history of Saddam Hussein,
and his willingness to terrorize himself."

--- Adolph Bush, Skull and Bones initiate,
Grand Rapids, Mich., Jan. 29, 2003

In an August 7, 2000 Time magazine interview,
George W. Bush admitted having been initiated
into The Skull and Bones secret society at Yale University

"...these same secret societies are behind it all,"

my father said. Now, Dad had never spoken much about his work.

--- George W. Bush
 
T

thufir

If the OP can grab a copy of the 1st edition Effective Java book,
there's an item that explains how to implement the Java enum manually,
for language versions that don't have the enum keyword. It adds some
insight to why Java enums work the way they do -- as classes in addition
to enums.


Actually, the first edition isn't as good as the second edition,
particularly for this question. The second edition gives two ways to
implement singleton and recomends the enum method.



-Thufir
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top