[英]Singleton Design Pattern and its child class's default constructor
In my coding, I use Singleton
class with Singleton Design Pattern
. 在我的编码中,我将Singleton
类与Singleton Design Pattern
。 Question is why its sub class does not allowed to use default constructor? 问题是为什么其子类不允许使用默认构造函数?
I get compile time error : 我得到编译时错误:
Implicit super constructor Singleton() is not visible. Must explicitly invoke another constructor
Singleton.java Singleton.java
public class Singleton {
private static Singleton singleton;
private Singleton() {
System.out.println("I am user class");
}
public static Singleton getInstance() {
if(singleton == null) {
singleton = new Singleton();
}
return singleton;
}
}
SubClass.java 子类
public class SubClass extends Singleton {
public SubClass(){
System.out.println("I am sub class");
}
}
When you create an instance of SubClass
then it automatically invokes the constructor
of its SuperClass
to initialize its fields, and that further invokes all the superclass constructors in the inheritance hierarchy
当您创建的实例SubClass
,然后它会自动调用constructor
它的SuperClass
初始化其领域,并进一步调用所有超构造函数在inheritance hierarchy
Now since your SuperClass
constructor is private, so it cannot invoke that. 现在,由于您的SuperClass
构造函数是私有的,因此它无法调用它。 So, you are getting that exception.. 因此,您将收到该异常。
But it doesn't make sense to subclass
a singleton
class, because in that case, your class will no longer be singleton
. 但是将singleton
类subclass
是没有意义的,因为在这种情况下, your class will no longer be singleton
。
You should re-think
about your design
and what you are trying to do. 您应该re-think
您的design
以及您想做的事情。 And change
your design accordingly. 并相应地change
设计。
SingleTon class are not supposed to be inherited so below ensures that 不应该继承SingleTon类,因此在下面确保
private Singleton() {
Below code 下面的代码
public SubClass(){
System.out.println("I am sub class");
}
Is same as : 与:
public SubClass(){
super(); //Error here as super class constructor is private
System.out.println("I am sub class");
}
Because your Singleton
class no-arg constructor is private
. 因为您的Singleton
类no-arg构造函数是private
。 When you instantiate sub-class it tries to instantiate super class also. 当实例化子类时,它也会尝试实例化超类。 But your super class has private constructor. 但是您的超类具有私有构造函数。 It fails. 它失败。
private Singleton() {
System.out.println("I am user class");
}
It doesn't make sense to subclass Singleton
class. 子类化Singleton
类没有任何意义。 You may need to re-think about your design. 您可能需要重新考虑您的设计。
First of all, subclassing a singleton class is breaking the basics of Singleton Pattern.
首先,单例类的子类化打破了Singleton Pattern.
的基础Singleton Pattern.
Secondly, due to the chain of calling the constructors through the inheritance tree, you get that exception since the constructor of the parent class is private
. 其次,由于通过继承树调用构造函数的链条,您将获得该异常,因为父类的构造函数是private
。
You will probably add a public constructor with some parameters to make the child inherit from its parent. 您可能会添加带有一些参数的公共构造函数,以使子代从其父代继承。 Then you will have maybe many instances of SubClass
. 然后,您可能会有许多SubClass
实例。 Any Subclass
instance IS-A Singleton
instance, too. 任何Subclass
实例都是IS-A Singleton
实例。 And this is against the purposes of Singleton pattern. 这与Singleton模式的目的背道而驰。 That's why you should avoid subclassing a singleton class. 这就是为什么应该避免子类化单例类的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.