• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sounds Like A Singleton

 
john gere
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, that does shed some light for me. Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic