singleton and inheritance in Java

前端 未结 6 640
鱼传尺愫
鱼传尺愫 2021-01-17 16:30

I have a base class that captures some functionality common to two classes. In other words, I can create one base class and make these two classes subclasses of that base cl

6条回答
  •  时光取名叫无心
    2021-01-17 16:47

    I'm not an expert in Java, so I don't know if this is technically legal Java code (perhaps another poster can comment):

    Make the base classes inherit from a generic class Singleton.

    Example:

    class Singleton {
    
        protected Singleton(); //constructor
    
        private static T _instance;
    
    }
    
    class DerivedOne extends Singleton{
    
        protected DerivedOne(){} //constructor
    }
    
    class DerivedTwo extends Singleton{
    
        protected DerivedTwo(){} //constructor
    }
    

提交回复
热议问题