Why use singleton instead of static methods?

I did not find good answers to these simple questions about helper / utility classes:

Why should I create singleton (stateless) instead of using static methods?

Why do we need an instance of an object if the object has no state?

+66
java design-patterns singleton
May 4 '10 at 12:03 a.m.
source share
7 answers

Syntax is used to inject some kind of global state into an application. If it has no state, I also see no reason to use singleton if

  • you expect to renew his condition for the foreseeable future or
  • you need an instance of the object for a specific technical reason (for example, for the C # lock operator, although this is already pretty far-fetched) or
  • you need inheritance i.e. You want to easily replace your singleton with another, using the same interface, but with a different implementation. For example, the Toolkit.getDefaultToolkit() method in Java will return a singleton whose exact type is system dependent.
+63
May 4 '10 at
source share

I could see the case of using a single, single element without using static methods, namely Injection of Dependency .

If you have a utility utility utility class that you use directly, it creates a hidden dependency; you cannot control who can use it, or where. Implementing the same helper class through a singleeless SingleStan instance allows you to control where and how it is used, and replace it with / mock / etc when you need to.

Creating a singleton instance simply ensures that you don't allocate more type objects than necessary (since you only need one).

+34
May 04 '10 at
source share

In fact, I found another answer that is not mentioned here: static methods are harder to test.

Most of the test frameworks seem to do very well with the mocking methods of instances, but many of them fail in a decent way due to static methods.

+9
Feb 25 '11 at 10:01
source share

In most programming languages, classes elude the type system. Although a class with its static methods and variables is an object, it very often cannot implement an interface or extend other classes. For this reason, it cannot be used in a polymorphic manner, since it cannot be a subtype of another type. For example, if you have an IFooable interface that is required for several method signatures of other classes, a StaticFoo object cannot be used instead of IFooable , whereas FooSingleton.getInstance() can (suppose FooSingleton implements IFooable ).

Note that since I commented on Heinzi's answer, the singleton is a template for instance control. It replaces new Class() with Class.getInstance() , which gives the Class author more control over instances, which he can use to prevent the creation of unnecessary instances. Singleton is just a special case of the factory pattern and should be considered as such. General use makes it more likely a special case of global registries, which often ends badly because global registries should not be used perforce.

If you plan to provide global helper functions, then static methods will work just fine. A class will not act as a class, but rather as a namespace. I suggest that you remain highly cohesive, or you may encounter the strangest communication problems.

Greetz
back2dos

+5
May 04 '10 at 12:34 a.m.
source share

There is a trade-off between using one. Singletones may or may not have a state, and they relate to objects. If they do not preserve state and are used only for global access, then static is better, since these methods will be faster. But if you want to use objects and concepts of OOP (inheritance polymorphism), then it is better to use singleton.

Consider an example: java.lang.Runtime is a singleton class in java. This class allows different implementations for each JVM. Implementation is uniform for JVM. If this class would be static, we will not be able to pass different implementations based on the JVM.

I found this link really useful: http://javarevisited.blogspot.com/2013/03/difference-between-singleton-pattern-vs-static-class-java.html ?

Hope this helps!

+2
Feb 21 '14 at 20:42
source share

For me, "Requires the use of a Singleton object object; Want Function uses a static method"

It depends on what you want. Whenever you want the state of an object (e.g. polymorphism like Null instead of null or the default state), singleton is the right choice for you, whereas the static method is used when you need a function (Receive input then return output) .

I recommend for the singleton case, it should always be the same state after its creation. It should not be cloned and take no values ​​for inclusion (except for the static configuration from the file, for example, a property file in java).

PS Performance between the two varies in milliseconds, so focus on Architecture first .

+1
Oct 10 '16 at 16:25
source share

Singleton is not stateless, it contains a global state.

Some of the reasons I can think of using Singleton are as follows:

  • To avoid memory leaks
  • Ensuring the same state for all modules in the application, for example, connecting to the database
0
May 04 '10 at 1:15
source share



All Articles