Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Singletons - I'm having a hard time understand how to implement them.

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Singletons - I'm having a hard time understand how to implement them.

    Heya. I'm pretty new at programming - currently following the BlueJ learning book.
    Our teacher is trying to beat some Singleton sense into our heads but I'm having a hard time going from theory to practice in one of my applications.

    I can change the "main" "runtime" class into a singleton using examples on the internet just fine - the problem arises when it comes to creating singletons of other classes from it.

    The main class is a console interface - a TUI if you will. Upon user request it will call a method to create an instance of another class (A specific interface) - Thing is those are supposed to be singletons as well. How do I go about ensuring they can't be made more than once? The main class runs the following method every time the user wants to change to the class (They are also console interfaces).

    Will changing the code in those classes to a singleton pattern fix it? The "new LibraryUI" bit is worrying me a bit.

    private void startLibraryUI()
        {
            libraryUI = new LibraryUI();
            libraryUI.start();
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Singletons - I'm having a hard time understand how to implement them.

    Post the full code for a sample class where you are trying to define it as a singleton.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Singletons - I'm having a hard time understand how to implement them.

    Make the constructor private and do something like this:

    public class Singleton
    {
     
    private Singleton()
    {
     
     
    }
     
     
    private static class SingletonHandler
    {
     
    static final Singleton instance = new Singleton();
     
    }
     
    public static Singleton getInstance()
    {
    return SingletonHandler.instance;
    }
     
     
     
     
    }

    Now the only way for stuff outside the class to get to it is by calling the getInstance method. Since the object is final, it can't be instantiated twice. However, if the object finally goes out of scope, then it can be instantiated again. (It will go out of scope if the window is closed or some other reason that would cause the object to "die" for lack of a better word.)

    However, and this goes for GUIs especially, you (probably, hey, there might perhaps be some cases where you DO want to do this) shouldn't do something like this (create a Singleton like that above)

    method.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent e)
    {

    s = Singleton.getInstance();


    }
    });

    Rather, do it before the ActionListener, if the GUI is a Singleton, and make it invisible initially and have the ActionListener make it visible. Doing it the former way makes a new, and separate somehow, instance of Singleton. How that happens, I'm not quite sure. But it does.

    I guess a way to think of it would be if you had a FontChooser for a tabbed Notepad. You might want to make sure that each Notepad is allowed only 1 FontChooser at a time. However, there should be nothing wrong for EACH Notepad in the tabs having (at most) one FontChooser, and it wouldn't violate the instance thing somehow.

    I don't know the exact mechanisms of how a Singleton works, though I DO know that scoping is part of that mechanism.

    I know that the Calendar (Java Platform SE 7 ) Calendar class in Java is a Singleton.

    In fact, it seems you can have multiple getInstance() methods.

    static Calendar getInstance()
    static Calendar getInstance(Locale aLocale)
    static Calendar getInstance(TimeZone zone)
    static Calendar getInstance(TimeZone zone, Locale aLocale)
    Last edited by javapenguin; February 16th, 2012 at 12:24 AM.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Singletons - I'm having a hard time understand how to implement them.

    Quote Originally Posted by javapenguin View Post
    M
    I know that the Calendar (Java Platform SE 7 ) Calendar class in Java is a Singleton.

    In fact, it seems you can have multiple getInstance() methods.

    static Calendar getInstance()
    static Calendar getInstance(Locale aLocale)
    static Calendar getInstance(TimeZone zone)
    static Calendar getInstance(TimeZone zone, Locale aLocale)
    Calendar is NOT an example of a Singleton. Once again feeding incorrect information. The Runtime class is a much better example of why you would want only a single instance of a class in an application, and is implemented as such.

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Singletons - I'm having a hard time understand how to implement them.

    Calendar isn't a Singleton?! Then why does it have a getInstance() method?

    What is the Runtime class for? It does indeed appear to be a Singleton.

    Calendar's constructor is protected, so maybe it isn't a Singleton, but it appears to be nearly a Singleton, as you can't access the constructor directly (unless you subclass Calendar.).
    Last edited by javapenguin; February 16th, 2012 at 05:36 PM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Singletons - I'm having a hard time understand how to implement them.

    With a singleton, you expect there to be only one instance.
    Call getInstance() twice and compare the references that are returned to see if they are the same.

  7. #7
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Singletons - I'm having a hard time understand how to implement them.

    Quote Originally Posted by javapenguin View Post
    Calendar isn't a Singleton?! Then why does it have a getInstance() method?

    What is the Runtime class for? It does indeed appear to be a Singleton.

    Calendar's constructor is protected, so maybe it isn't a Singleton, but it appears to be nearly a Singleton, as you can't access the constructor directly (unless you subclass Calendar.).
    Having a getInstance() method and/or a private/protected constructor does not define a Singleton - it is defined just as Norm has defined it: there exists one instance in an application.

Similar Threads

  1. Replies: 0
    Last Post: December 12th, 2011, 03:17 PM
  2. I am having a hard time with my code
    By SandeeBee in forum What's Wrong With My Code?
    Replies: 14
    Last Post: November 12th, 2011, 09:34 AM
  3. Having a hard time figuring out how to make my code work
    By iainnitro in forum Loops & Control Statements
    Replies: 2
    Last Post: September 6th, 2011, 07:48 AM
  4. I am having a hard time fixing this error
    By KuruptingYou in forum What's Wrong With My Code?
    Replies: 7
    Last Post: August 28th, 2011, 10:12 PM
  5. new to java.... having a hard time trying to read code
    By Newbie_96 in forum Java Theory & Questions
    Replies: 2
    Last Post: August 6th, 2011, 01:51 AM