Single classes

Is there a difference between a Singleton class and a class with all static elements (i.e. methods and attributes).

I could not find a single instance where the "entire static member class" does not achieve the same functions as the class that correctly uses the Singleton pattern?

For example, java.lang.Runtime is a suitable Singleton class, while java.lang.System has all the static access methods and just has a private constructor to avoid external builds. Does anyone know why classes like Runtime are made by Singleton and not implemented as java.lang.System .

Is it simply because it will be a cleaner design (i.e. more realistic imitation of the object) or is there a performance advantage here?

+8
java c ++ singleton
source share
8 answers

Yes, there is a difference - singleton can implement the interface.

In addition, what looks like a singleton from the outside can actually be implemented through different classes, where a single-threaded access method (for example, Runtime.getRuntime() ) can create the correct instance at runtime. I'm not saying what happened here, but this is an option.

+15
source share

Well, you can serialize and non-serialize an object (and therefore Singleton) using the Serializable interface (in Java), but not a static class.

+3
source share

One instance is created once.

A static class is never created.

+1
source share

The main goal for GoF singles is to provide a polymorphic service, where singleton is an abstract base class, and a particular type is determined at runtime. And, of course, there should only be one of them in the program.

0
source share

I believe that there is no difference between what you call singleton and a class with all the static methods / members in principle. In fact, I think that creating a class with all the static members is a way to implement the idiom of the same name. Well, maybe there is some serious difference in Java, but I am speaking from the point of view of C ++.

0
source share

I think you should ask what distinguishes the final class from the private constructor and the static class. Since singleton is a class, the implementation depends on the programmer who programs this class. This is the same as asking what the differences are between an object and a static class.

0
source share

The class can be extended to create another singleton (for example, for testing purposes) or a non-singleton class. static methods cannot be overridden; they can only be hidden in a subclass.

0
source share

A common use for single games with lazy initialization (aka Meyers singleletons) is to control the initialization order of static objects (which in C ++ is undefined for different translation units). In this regard, singleton simply behave like global objects, but the order of their construction behaves well.

It is difficult to control the order of destruction. If you must rely on individual nodes to be destroyed in a specific order (for example, one of the journal classes that must survive the other instances of the same instance), see the Book of Alexandrescu to testify to the difficulty.

0
source share

Source: https://habr.com/ru/post/650153/


All Articles