Results 1 to 11 of 11
  1. #1
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    95
    Rep Power
    0

    Question What is a singleton and how to implement one?

    Ok I had a similiar thread about his before, however I don't think I explaned very well exactly what it is I'm trying to do. Firstly, what is a singleton(is it like a variable?) Also how would I access it from another class? I would think you would use getters and setters right? Also if I wanted a singleton to be an int how would I do that? I am trying to make a singleton object and trying to access it elsewhere. I want my singleton object to be money. I want its value to change depending on what I buy in a store or sell, etc. This is what it looks like so far but I know it is incomplete and faulty:


    Java Code:
    public class Money  {
        
       private static Money bank= new Money(); //I want money to be an int
       
       public static Money getMoney() { 
           return bank; }
       
       private Money() {
         //this would be my default constructor, I am not sure want to do here yet but I want money to start out as 0 and change depending on what I buy or sell
       } 
    }
    
    
    
    
    public class store extends javax.swing.JFrame{
    private int bank;
    private store(){
    initComponents();  //this is a store JFrame
    Money.getBank();  //I think this is how I access it
    bank=Money.getBank();  //I don't know how to implement Money
    }
    
    private void EmploymentActionPerformed(java.awt.event.ActionEvent evt) {               
    {
    //I want this to add money                      
            workFields= new WorkFields();  //A JFrame
            workFields.setVisible(true);
            bank = bank + 10;
            bankLabel.setText("Gold: " + bank);
            }
    
    private void StoreActionPerformed(java.awt.event.ActionEvent evt) {
    if (Store.getSelectedItem().equals("Map")) {
      //I want this to subtract money
                Dialog.showMessage("You bought a world map");
                bank = bank - 10;
                bankLabel.setText("Gold: " + otherbank);
                Store.removeItem("Map");
    I hope you all can understand what I am trying to do. Thank you

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,534
    Rep Power
    5

    Default Re: What is a singleton and how to implement one?

    A singleton is nothing more than an instantiation of a class which always returns the same object. The singleton is usually created by a static method in the class like, getInstance(). That method usually lazily creates the singleton and returns the same object over and over again. The best approach is to use an enum to create you singleton.

    It appears to me that you want a bank to be a singleton so that whenever some code in your program gets the bank, then they all get the same bank. Then internally to that bank you would need some data structures to handle accounts, etc. You may even want another class, perhaps inner class to bank which creates those accounts.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: What is a singleton and how to implement one?

    Imagine this: that (single) Money object has a (private) int member variable and a public getTheInt() method (and possibly a setTheInt(int theInt) method). All you have to do is call the public Money.getMoney() method to get the singleton object and call the method getTheInt() on it to get your wanted int value ...
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    95
    Rep Power
    0

    Default Re: What is a singleton and how to implement one?

    So if I create an enum like this:
    public enum Bank {
    bank;
    }
    then how would I access that in another class? Are singletons similar to enums?

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: What is a singleton and how to implement one?

    You still need to define that int member variable in that enum and the method(s) to access that int.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    95
    Rep Power
    0

    Default Re: What is a singleton and how to implement one?

    Do you define enums the same way as variables?

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: What is a singleton and how to implement one?

    Quote Originally Posted by Snake006 View Post
    Do you define enums the same way as variables?
    Have you read a tutorial on the subject? It tells you all. There is no use for us to regurgitate it all here again.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    95
    Rep Power
    0

    Default Re: What is a singleton and how to implement one?

    Ok I have looked up several tutorials for singletons. I have made some progress but I still am not sure how to use my singleton in the class I want to use it in. I think my singleton is complete I am just not sure how to use it:
    public enum Bank {
    Bank;


    private int amount;
    Bank(int amount){
    this.amount=amount;

    }

    private int getAmount(){
    return amount;
    }

    } //end of singleton

    //I am not sure how to use my singleton
    Town class(){
    static Bank bank;
    public town(Bank bank){
    initComponents();
    this.bank=bank;
    }
    }

    I modeled my code off of the Java oracle tutorial example that was given. I understand how to get text to work, just not numbers

  9. #9
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    95
    Rep Power
    0

    Default Re: What is a singleton and how to implement one?

    Ok I actually firgured it out

  10. #10
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    95
    Rep Power
    0

    Default Re: What is a singleton and how to implement one?

    I needed to do this instead:
    public enum Bank {
    theBank(0);


    private int amount;


    Bank(int amount){
    this.amount=amount;

    }

    public int getAmount(){
    return amount;
    }

    }
    By doing this it set my bank to 0 in my town class by saying Bank.theBank.getAmount(); in my constructor.
    My next and final question is, how do I add to that value in the town class? Bank.theBank.getAmount()+a number doesn't cut it and I didn't think it would. It says a variable is required, which I have one for the town class called otherbank but I can't set Bank.theBank.getAmount() equal to it.

  11. #11
    Snake006 is offline Member
    Join Date
    Jun 2013
    Posts
    95
    Rep Power
    0

    Default Re: What is a singleton and how to implement one?

    Wait never mind I figured that out too. Bank.theBank.getAmount(otherbank); works if I make the method getamount() require an int. Now I can add and subtract with ease. I can also do that in my port class although I'm begining to wonder if I needed a singleton at all since in my port class I call Town.otherbank and that gives me the value leftover from my spendings.

Similar Threads

  1. Replies: 1
    Last Post: 09-07-2011, 06:08 AM
  2. Singleton
    By dardar in forum New To Java
    Replies: 2
    Last Post: 09-20-2010, 10:15 AM
  3. Replies: 2
    Last Post: 10-06-2008, 09:06 PM
  4. Replies: 0
    Last Post: 03-29-2008, 12:41 PM
  5. Replies: 0
    Last Post: 09-26-2007, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •