This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi, I have a dataclass that I want to be a singleton class. This is how it looks...
This part works okay. I get one instance and use it to access the methods I need. The problem is I want to get a snapshot of the data each time the load method is called and dump it into an array list. I want each item in the list to store the different member values that were loaded. I am reading the values from a file. This is the way I am attempting to store a snapshot of the SingletonClass each time its members are updated with the load() method.
My first load may have loaded (100) and my second load(200) but if I call "ArrayList.get( 0 )" the value for load 2(200) is returned. The reason I went this way is because it processed 100000 lines of pipe-delimited values in about 2234 ms. and I don't have to create a new dataclass instance for each line. Is there something I am leaving out? Is this a possibilty using the Singleton approach? Thanks for you help and patience. [ January 13, 2004: Message edited by: Chris Ramsey ]
You're seeing the last value from every entry in the arraylist because every time you add the very same object (the single instance) to the arraylist. You might wind up with a hundred references to the same object and they'll all give the same result. So, you don't want to store the SingletonObject itself in the arraylist, but the current value that's in it. You want something more like:
Does that make sense? Bad news is it doesn't work yet. You can't add an int to an ArrayList because it's not an object. So maybe add new Integer(gettheint...). Something to ask yourself about any singleton, does it really have state that we need to keep? The only value being kept for the long term are in the array list so the answer seems to be no. In that case, you can use all static methods and variables and never create an instance at all. Of course you may have other things going on that make the singleton more interesting. Either approach - singleton or static - can run into trouble if you have multiple threads. Probably not a problem today, but something to keep in mind when you get to threads. Then you'll have to synchronize methods or give up and make mulitple instances. Last note: Your getInstance method tests for null, but it will never be null because you created an instance along with the declaration. That's a good way to do it! You can just remove that test. Hope all that helps!
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Sep 15, 2003
Thank you Stan. Yes that makes sense. I should have caught that. So maybe I should do something like...
This will work. The rest of the program will parse the line to some textfields. I guess I would have to create the string at some point, but atleast it will be ready to present. I just didnt want to create a whole bunch of strings. Thank you very much for responding. [ January 13, 2004: Message edited by: Chris Ramsey ]