5

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??

Anwar Shaikh
  • 1,591
  • 3
  • 22
  • 43

4 Answers4

4

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:

  • Use a free-standing function enclosed in a C++ namespace
  • Use an extension class in C#
  • Use a helper class in Java
  • Use a Category in Objective-C
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I am using c++ language i am thinking of creating a wrapper class of only static methods which won't inherit from the singleton Logger class. How is it?? And will you please elaborate on method you described above for c++. Thanks.. – Anwar Shaikh Mar 26 '12 at 18:03
  • @AnwarShaikh That works too - this is what I would do in Java, because free-standing functions are not available there. I'd go that route in C++ as well, but only if I needed to share state or other implementation details among the functions that I'm adding. – Sergey Kalinichenko Mar 26 '12 at 18:05
  • Thank You!! I will try by writing a wrapper over it. – Anwar Shaikh Mar 26 '12 at 18:20
1

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.

Garrett Hall
  • 29,524
  • 10
  • 61
  • 76
  • Yes sure singletons need to be avoided, but it is turns out to be good when used for something like Logger. what if I need some additional methods along with those are in singleton should I inherit it or write wrapper(of class with only static methods) over singleton which internally uses the singleton. – Anwar Shaikh Mar 26 '12 at 18:36
  • Go for a Wrapper. That's what I meant by 'delegate calls'. – Garrett Hall Mar 26 '12 at 19:06
1

I do agree with Garret Hall, you should avoid Singleton's if possbile. That said, I don't see it as wrong for one reason. The person that implemented the Singleton let you extend it. If the api developer didn't want the singleton class Logger to be extended, they would have made the constructor private, in C++ for instance, or whatever method is appropriate for the language you are using.

nathan
  • 5,513
  • 4
  • 35
  • 47
0

I think writing a wrapper over the class is not the proper solution, because you need to write wrapper for every method you need from singleton. And you end up in writing dozens of functions.

Also you are restricting yourself from using all the functions available in singleton.

NOT A GOOD IDEA!!!