I decided to use the
Singletonpattern for general development, it didn’t work out much)). Despite the abundance of descriptions, I did not understand if it gives any advantage compared to, say, an ordinary abstract class? So I got the impression that there are more inconveniences, and the usual abstract class does all the same. Maybe I just don’t know how to cook it?
Answer 1, authority 100%
An abstract class cannot have instances at all. It is usually used as the basis for further inheritance. A singleton always has one instance. Sometimes it is used, for example, to create a connection to the database, or other entities that the application needs in a single instance.
Answer 2, authority 50%
And these are slightly different things. A singleton is a single instance of a class. And an abstract class can have more than one instance (as implementations of child classes).
Since a singleton is a pattern, it can be replaced by other functionality, and the decision to use one or another pattern is up to the developer.
Singleton is recommended when there should be one and only one object, for example, a pool of connections to the database, application settings, etc.
I’ll clarify (rather for myself, since the answer to the question has already been given)
Singleton– means that there can be only one instance of a class. One and all here. There can be no “many” in principle. For example, as mentioned above, the instance that stores the connection to the database (if there is only one database).
Abstract classis an ancestor of a class structure that has descendants. “Singleton” has NOTHING to do with the reception of programming directly. This is a completely different entity and this class has a completely different purpose – to define an interface for interacting with all instances of the descendants of this class. BUT. Nobody bothers to design an abstract class using the “Singleton” pattern. For example, define an abstract class that will store ONE instance of a database connection AND this instance can be of the type of one of the descendants of this abstract class – connection via ODBS, connection to SQL, connection to Oracle – as options.