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?

Angelo Alvisi
  • 480
  • 1
  • 4
  • 15
R D
  • 1,330
  • 13
  • 30
  • You are using eager initialization of your data structure (Say List of candidates) which seems similar to Singleton. So what your question is? – SMA Feb 01 '15 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? – R D Feb 01 '15 at 16:26
  • Have you seen how Singleton implemented? It's worth you know how singleton is implemented and then you compare with yours. – SMA Feb 01 '15 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. – R D Feb 01 '15 at 16:44

1 Answers1

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:

R D
  • 1,330
  • 13
  • 30
Kostya Buts
  • 1,567
  • 1
  • 13
  • 14
  • Your 4th link `Singleton design pattern...` lead me to a virus / ad infested website. – Rien Dec 12 '17 at 13:43