I am creating a logger for an application. I am using a third party logger library. In which logger is implemented as singleton.
I extended that logger class because I want to add some more static functions. In these static functions I internally use the instance (which is single) of Logger(which i inherited).
I neither creates instance of MyLogger nor re-implemented the getInstance() method of super class. But I am still getting warnings like destructor of MyLogger can not be created as parent class (Loggger) destructor is not accessible.
I want to know, I am I doing something wrong? Inheriting the singleton is wrong or should be avoided??
Hence, you can extend a singleton class only if you have a non-private constructor in the singleton class as shown in the code snippet given below. You can now extend the singleton class as shown below. Likewise, you cannot inherit a static class and override its methods.
The most important drawback of the singleton pattern is sacrificing transparency for convenience. Consider the earlier example. Over time, you lose track of the objects that access the user object and, more importantly, the objects that modify its properties.
Criticism. Critics consider the singleton to be an anti-pattern that introduces global state into an application, often unnecessarily. This introduces a potential dependency on the singleton in all code it is visible to, requiring analysis of implementation details to determine if a dependency actually exists.
A common mistake with that implementation is to neglect synchronization, which can lead to multiple instances of the singleton class.
Leaving the merits of singleton pattern aside (there is a school of thought that describes it as an anti-pattern) it should not be necessary to subclass it to simply add static functionality. Here are language-specific approaches that I would prefer to subclassing a singleton:
I would use a non-singleton and delegate calls to the singleton where needed. Whenever you have the opportunity to get rid of a Singleton, go for it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With