2

I have developed a "Quiz application in java", which have many features like quiz conduction, instant reporting features, students can post their doubts and all.

I want to maintain the log of the number of students requested for quiz, the number of students that submitted the response, the total active students and some other details.

I am using a Global class which has static variables to keep these details. Is it correct way to maintain this or should I want to use the Singleton design pattern ?

Kindly give me pros and cons of both and which one to use and when?

4
  • You are using eager initialization of your data structure (Say List of candidates) which seems similar to Singleton. So what your question is? Commented Feb 1, 2015 at 16:19
  • @ almas shaikh, as for now im using static instant to keep these details inside a class, is it correct way im doing r i have to use singleton DP? Commented Feb 1, 2015 at 16:26
  • Have you seen how Singleton implemented? It's worth you know how singleton is implemented and then you compare with yours. Commented Feb 1, 2015 at 16:27
  • Thanks, @almas shaikh, Yes while seeing the singleton implementation only i got this question in my mind. That's why i raised this question which is best to use in my case. Commented Feb 1, 2015 at 16:44

1 Answer 1

5

Long story short: by using global variables you're breaking OOP rules (encapsulation). You will keep using them until your code becomes one big mess. So instead of using global variables I would go for singleton (but make sure it is thread safe). Neither solutions are great thought.

P.S. There is a lot of information on the internet:

Sign up to request clarification or add additional context in comments.

1 Comment

Your 4th link Singleton design pattern... lead me to a virus / ad infested website.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.