implementing Singleton using enums

Discussion in 'Java' started by puzzlecracker, Jul 22, 2008.

  1. 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
     
    puzzlecracker, Jul 22, 2008
    #1
    1. Advertising

  2. puzzlecracker

    Jeff Higgins Guest

    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>
     
    Jeff Higgins, Jul 22, 2008
    #2
    1. Advertising

  3. puzzlecracker

    Jeff Higgins Guest

    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>
     
    Jeff Higgins, Jul 22, 2008
    #3
  4. In article
    <>,
    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");
    > }
    > }


    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
     
    John B. Matthews, Jul 22, 2008
    #4
  5. puzzlecracker

    Roedy Green Guest

    On Mon, 21 Jul 2008 23:37:04 -0400, "Jeff Higgins"
    <> 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
     
    Roedy Green, Jul 22, 2008
    #5
  6. puzzlecracker

    Mark Space Guest

    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.
     
    Mark Space, Jul 22, 2008
    #6
  7. puzzlecracker

    Lew Guest

    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
     
    Lew, Jul 22, 2008
    #7
  8. puzzlecracker

    thufir Guest

    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
     
    thufir, Jul 23, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,198
    Keith Thompson
    Jan 20, 2007
  2. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,337
    Proton Projects - Moin
    Mar 27, 2007
  3. Wilhelm
    Replies:
    1
    Views:
    187
  4. Trans
    Replies:
    12
    Views:
    304
    Robert Klemme
    Sep 14, 2007
  5. Paul McMahon
    Replies:
    3
    Views:
    223
    David A. Black
    Jun 9, 2008
Loading...

Share This Page