3

In the quest for more opinions I rewrite the question (I´m learning how to ask and English is not my mother tongue)...

Is it redundant or best practice to keep all the methods and global vars as static? (I mean there´s only one instance already per se)

Whimusical
  • 6,401
  • 11
  • 62
  • 105

5 Answers5

2

If none of the methods depend on the state (the instance attributes) of the class, then you don't need a singleton, simply declare them all as static - you'll have an utility class then (that's the second approach proposed in the question).

On the other hand, if the methods do depend on the state of the class, and you must ensure that only one instance of the class exists at any moment in time, then use a singleton (that's the first approach suggested in the question).

Notice that the second approach is not really considered a singleton, by definition a singleton is a pattern "used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object", and a class with all-static methods doesn't need to be instantiated at all.

EDIT :

Regarding the invocation of static methods in a singleton class, it's considered bad style to invoke static methods on an object instance, it doesn't matter if it is a singleton or not. This has been discussed extensively in previous posts. So for consistency, I believe it'd be better to declare all the methods in a singleton as non-static, even if they don't depend on instance attributes, and access all of them through the singleton (the first approach in your question).

Community
  • 1
  • 1
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • 1
    I must add to the comment that the second options doesn't fit to the Singleton pattern definition. – Israel May 08 '12 at 11:53
  • @FidoX yep, I was going to expand a bit on that, you commented before I was finished editing :) – Óscar López May 08 '12 at 11:55
  • This is the answer. I would also like to add that if you just use static methods then what's the purpose of the singleton pattern? If all you have is static methods in there you wont be calling them on the instance anyway. – kentcdodds May 08 '12 at 11:56
  • How does the need for state motivate Singleton? Why not use static class variables? – Marko Topolnik May 08 '12 at 11:57
  • 1
    @MarkoTopolnik static class variables won't allow for lazy instantiation, which might be a desirable property when using a singleton – Óscar López May 08 '12 at 11:59
  • So the real motivator is lazy initialization. It's better to state it that way, especially since a lazy singleton is not the first thing that comes to mind and has a radically different initialization idiom. – Marko Topolnik May 08 '12 at 12:01
  • But Im not asking static class vs singleton, but static vs non-static methods given a singleton when applicable, as I reformulate in the EDIT. – Whimusical May 08 '12 at 12:50
  • @user1352530 I just updated my answer for answering the edit in your question – Óscar López May 08 '12 at 13:02
  • I know, but if the behavior is the same, I don´t have any need for making it static. What I ask is what is preferred between making the method static and calling it the second way or making it non-static and call it the first. The method have no dependencies on instance variables. Gracias Oscar, la respuesta esta cerca (no tengo acentos en el teclado). – Whimusical May 08 '12 at 13:18
  • @user1352530 OK! I edited my answer again. For consistency's sake, I'd go with the first approach. Por nada! – Óscar López May 08 '12 at 13:24
  • Thank you Oscar, you hit the point! I accept this answer as long as it´s the only one that solves totally my doubt – Whimusical May 08 '12 at 13:27
1

If a class makes use of any resources or is costly to initialize a singleton is usually worth the effort.

  1. A singleton will give you much better control over the lifetime of the object. Static constructors are guaranteed to be called before any static methods are accessed however there is no way to force a static constructor to run again.

  2. Singleton objects are easier to test, there is no way to have an interface over static methods.

  3. A singleton can easily be converted to another implementation pattern, perhaps a pool of resources as opposed to a single object.

Jason C
  • 142
  • 3
  • 1
    I'll add to your comments about static initializers the fact that if two classes have them and mutually refer to each other, you cannot easily predict which order they run in. They are therefore inappropriate for many applications. – Jules May 08 '12 at 12:06
1

The advantage of a singleton with instance methods over a class with static methods is that it can

  • extend another class
  • implement an interface
  • be passed as argument to methods

This makes a big difference, especially if you want to unit test methods depending on this singleton: you may pass another, mock instance of the singleton implementing the same interface.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • +1. I get tired of seeing advice to just use static methods: a singleton with virtual methods makes code *much* easier to test, because you can test the code using a different implementation if you need to. And changing to it later can be hard, so it is always best to use a "proper" singleton rather than static methods, just in case you need to do this later. – Jules May 08 '12 at 12:02
0

I think it's a "philisophical question".

If it's just a method I call frequently and without other call to the singleton, I prefer the second way.

MyClass.doSomethingElse()
Kev
  • 118,037
  • 53
  • 300
  • 385
X-Blaster
  • 1,751
  • 3
  • 15
  • 32
0

If you can solve your problem using static methods, then do it that way. It's simpler and shorter to write since you don't need to fetch the singleton instance. However, these are the typical cases why you would want a Singleton:

  • lazy initialization;
  • polymorphism (dynamic dispatch -- decide at runtime which behavior you want, implement existing interfaces, mock for tests, ...);
  • a scope different than global for the singleton (perhaps on a per-thread basis).

The main point is, if you need any of those or anything similar, you'll know why the static utility class is not applicable. Then upgrade to Singleton.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • 1
    If you declare any method static, then it's no longer an instance method of the singleton -- you are circumventing it entirely. You don't need any instance of the class to call a static method and if you use the syntax `singletonInstance.staticMethod`, that's just a roundabout way to write `SingletonClass.staticMethod`. – Marko Topolnik May 08 '12 at 12:53
  • Exactly, thats why I put the same example as you in the question, it varies only in the way of calling it. And then, whats nicer/best practice if you found yourself in that situation? – Whimusical May 08 '12 at 13:02