File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Sounds Like A Singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sounds Like A Singleton" Watch "Sounds Like A Singleton" New topic
Author

Sounds Like A Singleton

john gere
Greenhorn

Joined: Mar 10, 2011
Posts: 22
I have a method that generates an ArrayList of questions. Here is the code:

When a user performs a mouse click action I want to call a method that uses this method to produce an array list of questions, but only once. It makes no sense every time the user clicks, the actioned performed involves creating a new instance of the above method to get an arraylist. The instance must only be created once. A singleton pattern is usually used to create something once, but I am not sure how to do this. How do I achieve what I am looking for?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

It isn't clear what you really need to do only once in that code. But if you only want something done once, then don't put it in a method which is called repeatedly. And if you only want one instance of something in your class, then just make an instance variable in the class which refers to that instance. You don't need to mess around with singletons. This might be all you need:

john gere
Greenhorn

Joined: Mar 10, 2011
Posts: 22
Paul Clapham

That is exactly what I want to do and I'm having difficulty. What you did there:

I can't just do that cause I'll have to put it in a try {} catch {} block. While I have the opportunity I'll try to be a little clearer. The GetQuestions() method produces an ArrayList with 10 items. 2 questions and 4 answers each. So question 1, followed by 4 possible answers, then question 2 followed by 4 possible answers. The GetQuestions() method puts this into a nice arraylist. However what I want to do is, when a user clicks a button only one question shows at a time. To achieve this I intended the following, every time the user clicks the button, the method ShowQuestions() is called. This however only iterates the first 5 items of the ArrayList. Then before the method concludes the first 5 items are deleted. So when the method is called again, which will be the second button click the method then only shows the second questions which will be the next 5 items. This is what I am trying to achieve and not to sure how to get it. Hope this clears things up. Having said that, what should I do?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18985
    
    8

First of all it looks like your data design is a bit deficient. You are mixing questions and answers in the same data structure, whereas in reality each question has a list of answers and your application has a list of questions.

So then when the button was clicked, the ShowQuestions() method would get the first question from the list, remove it from the list, and display it. But it sounds like you're already doing that, so I don't see what the problem is.

Anyway if your design says you should generate all the questions before you let the user start answering them, then you should just do that. As for your question about catching the exceptions, you can write an init() method which generates the questions and deals with the exceptions.
john gere
Greenhorn

Joined: Mar 10, 2011
Posts: 22
Ok, that does shed some light for me. Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sounds Like A Singleton