Given singleton: the need for static methods and vars?

In search of a bigger opinion, I rewrite the question (I teach how to ask, and English is not my native language) ...

Is it redundant or best practice to keep all methods and global vars static? (I mean, only one instance is on its own)

+3
source share
5 answers

If none of the methods depends on the state (instance attributes) of the class, then you do not need a singleton, just declare them all as static- then you will have a utility class (which is the second approach proposed in the question).

, , , , ( , ).

, , singleton - , singleton, , ", all- static .

EDIT:

, , , , . . , singleton not static, singleton ( ).

+2

, .

  • . , - , .

  • Singleton , .

  • , , , .

+1

singleton ,

, unit test : , mock singleton, .

+1

, " ".

, , .

MyClass.doSomethingElse()
0

, . , singleton. , , :

  • ;
  • ( - , , , ,...);
  • , singleton (, ).

, - - , , . Singleton.

0

All Articles