Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > implementing Singleton using enums

Reply
Thread Tools

implementing Singleton using enums

 
 
puzzlecracker
Guest
Posts: n/a
 
      07-22-2008
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
 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      07-22-2008

puzzlecracker wrote:
> 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>


 
Reply With Quote
 
 
 
 
Jeff Higgins
Guest
Posts: n/a
 
      07-22-2008

Jeff Higgins wrote:
>
> puzzlecracker wrote:
>> 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>
>


<http://www.ddj.com/java/208403883?pgno=3>


 
Reply With Quote
 
John B. Matthews
Guest
Posts: n/a
 
      07-22-2008
In article
<(E-Mail Removed)>,
puzzlecracker <(E-Mail Removed)> wrote:

> 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>

--
John B. Matthews
trashgod at gmail dot com
home dot woh dot rr dot com slash jbmatthews
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-22-2008
On Mon, 21 Jul 2008 23:37:04 -0400, "Jeff Higgins"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone who
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.


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.
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      07-22-2008
Jeff Higgins wrote:

>
> <http://www.ddj.com/java/208403883?pgno=3>
>


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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      07-22-2008
Roedy Green wrote:
> 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

 
Reply With Quote
 
thufir
Guest
Posts: n/a
 
      07-23-2008
On Tue, 22 Jul 2008 09:49:55 -0700, Mark Space wrote:


> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Enums without identifier, enums and typedef =?utf-8?b?QXNiasO4cm4gU8OmYsO4?= C Programming 10 01-20-2007 01:20 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM



Advertisments