Is it possible to apply inheritance to a Singleton class?

前端 未结 8 1975
孤独总比滥情好
孤独总比滥情好 2021-01-30 23:05

Today I faced one question in interview. Is it possible to apply inheritance concept on Singleton Classes? I said since the constructor is private, we cannot extend that Singlet

相关标签:
8条回答
  • 2021-01-30 23:30

    A private constructor is visible to other inner classes of that singleton class. So yes, technically a singleton class with a private constructor can be extended by its inner class. But why would you do something like that is beyond me.

    0 讨论(0)
  • 2021-01-30 23:34

    Yes, it is technically possible, as singleton is a design pattern and not a language construct that could have inheritance restrictions. I would just reimplement the public [Object] getInstance() method in the child classes (see below).

    And, yes, singletons can also benefit from inheritance as they may share similar but not identifical behaviours with other singletons.

    public class ParentSingleton {
    
        private static ParentSingleton instance;
    
        protected ParentSingleton() {
        }
    
        public static synchronized ParentSingleton getInstance() {
           if (instance == null) {
              instance = new ParentSingleton();
           }
    
           return instance;
        }
    
        public int a() {
           // (..)
        }       
    }
    
    public class ChildSingleton extends ParentSingleton {
    
        private static ChildSingleton instance;
    
        public static synchronized ParentSingleton getInstance() {
           if (instance == null) {
              instance = new ChildSingleton();
           }
    
           return instance;
        }       
    }
    

    EDIT: as Eyal pointed out in his comments below, the constructor in the super class had to be made protected (instead of private), otherwise it would not be visible to the child classes and the code would not even compile.

    0 讨论(0)
  • 2021-01-30 23:35

    Citing the bible:

    Use the Singleton pattern when [...] the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

    The Singleton pattern has several benefits: [...] 3. Permits refinement of operations and representation. The Singleton class may be subclassed, and it's easy to configure an application with an instance of this extended class. You can configure the application with an instance of the class you need at run-time.

    As for how to implement this: the book suggests several way, the most sophisticated of which is a registry in which instances are looked up by name.

    0 讨论(0)
  • 2021-01-30 23:40

    You could make the constructor package-private. This way, no classes outside the package could instantiate the Singleton class but derived classes could call the upper class constructor.

    But, as others said, this is really not advisable.

    Stupid interview question by the way.

    0 讨论(0)
  • 2021-01-30 23:43

    Its 'possible' to hack anything together really, but in this case it is not really advisable. There's no real reason to use the singleton pattern with inheritance, its just not meant for it.

    0 讨论(0)
  • 2021-01-30 23:43

    Below is one implementation of singleton pattern explained in GOF about creating singleton with inheritance. Here the parent class should be modified to add a new derived class. Environment variable can be used to instantiate appropriate derived class constructor.

    Mainfile – 1
    #include <iostream>
    #include <string>
    #include "Singleton.h"
    using namespace std;
    
    int main(){
    
         Singleton::instant().print();
         cin.get();
    }
    Singleton.h
    #pragma once
    #include <iostream>
    using std::cout;
    class Singleton{
    public:
        static Singleton & instant();
        virtual void print(){cout<<"Singleton";}
    protected:
        Singleton(){};
    private:
        static Singleton * instance_;
        Singleton(const Singleton & );
        void operator=(const Singleton & );
    
    };
    
    Singleton.cpp
    #include "Singleton.h"
    #include "Dotted.h"
    Singleton * Singleton::instance_ = 0;
    Singleton & Singleton::instant(){
        if (!instance_)
        {
            char * style = getenv("STYLE");
            if (style){
                if (strcmp(style,"dotted")==0)
                {
                    instance_ = new Dotted();
                    return *instance_; 
                }           else{
                    instance_ = new Singleton();
                    return *instance_;
                }       
            }
    
            else{
                instance_ = new Singleton();
                return *instance_;
            }
        }
        return *instance_;
    
    }
    Dotted.h
    
    #pragma once
    class Dotted;
    
    class Dotted:public Singleton{
    public:
        friend class Singleton;
        void print(){cout<<"Dotted";}
        private:
            Dotted(){};
    
    };
    
    0 讨论(0)
提交回复
热议问题