我应该为我的单例类创建受保护的构造函数吗?

发布于 2024-10-06 12:38:13 字数 471 浏览 5 评论 0原文

根据设计,在单例模式中,构造函数应标记为私有,并提供一个创建方法来重新调整同一类型实例的私有静态成员。我只像这样创建了我的单例类。

public class SingletonPattern {// singleton class

    private static SingletonPattern pattern = new SingletonPattern();

    private SingletonPattern() {

    }

    public static SingletonPattern getInstance() {
        return pattern;
    }

}

现在,我必须扩展一个单例类来添加新行为。但私有构造函数不允许定义子类。我正在考虑将单例基类的默认构造函数更改为受保护的构造函数。

如果我将构造函数定义为受保护,可能会出现什么问题?

寻求专家意见....

By design, in Singleton pattern the constructor should be marked private and provide a creational method retuning the private static member of the same type instance. I have created my singleton classes like this only.

public class SingletonPattern {// singleton class

    private static SingletonPattern pattern = new SingletonPattern();

    private SingletonPattern() {

    }

    public static SingletonPattern getInstance() {
        return pattern;
    }

}

Now, I have got to extend a singleton class to add new behaviors. But the private constructor is not letting be define the child class. I was thinking to change the default constructor to protected constructor for the singleton base class.

What can be problems, if I define my constructors to be protected?

Looking for expert views....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

廻憶裏菂餘溫 2024-10-13 12:38:13

如果您通过继承扩展单例类,那么如果有人获取您的单例和原始单例,您将有 2 个单例类实例在运行。

如果原始单例在概念上确实应该是单例,那么使用组合可能是正确的选择。然而,可替代性就会丢失(你的类不能替代原始的单例;它只是使用它)。

你有具体的例子吗?

If you extend a singleton class via inheritance, you'll have 2 instances of the singleton class running around should someone grab your singleton and the original singleton.

If the original singleton is conceptually really supposed to be a singleton, then using composition is probably the way to go. However, then substitutability is lost (your class is not substitutable for the original singleton; it just uses it).

Do you have a concrete example?

成熟的代价 2024-10-13 12:38:13

如果你这样做,它就不是单身人士。但也许你并不真正需要单身人士。

If you do that, it's not a singleton. But perhaps you don't really need a singleton.

梦回旧景 2024-10-13 12:38:13

这不是单例类。想象一下,我可以调用 getInstance() 静态方法 n 次,并且我可以拥有该类的 n 个对象,从而完全违反了单例模式。要使其成为单例,您应该在 getInstance() 方法中检查对象是否已创建。如果已经创建,那么您应该忽略并且不要再次创建。例如,你可以这样类似的东西,请忽略语法错误,只是一个代码来解释,在不同的语言中可能会有所不同。

public class SingletonPattern {// singleton class

private static SingletonPattern pattern = new SingletonPattern();

private SingletonPattern() {

}

public static SingletonPattern getInstance() {
    if(SingletonPattern == null) {
        return new SingletonPattern();
    }
}

This is not the Singleton Class. Imagine I can call getInstance() static method n number of times and I can have n objects of this class thus completely violating Singleton Pattern. To make it Singleton you should check whether object is already created or not in getInstance() method. If already created then you should ignore and do not create again. For example, you can so something similar, please ignore syntax mistakes, just a code to explain, can vary in different languages.

public class SingletonPattern {// singleton class

private static SingletonPattern pattern = new SingletonPattern();

private SingletonPattern() {

}

public static SingletonPattern getInstance() {
    if(SingletonPattern == null) {
        return new SingletonPattern();
    }
}
自我难过 2024-10-13 12:38:13

我知道的老问题,但碰巧发现了这个,我想我可以添加一些有用的东西。

单例类中可以有受保护的构造函数。如果您想在 Singleton 上具有多态行为,您可以将其设为抽象类,将构造函数设置为受保护,并将实例的创建委托给具体子类之一。

我在《设计模式详解》一书中找到了以下例子:

abstract public class Tax{
    static private Tax instance;

    protected Tax() {};

    abstract double calcTax( double qty, double price);

    public static Tax getInstance() {
      // code to determine what implementing class to use
      instance = USTax.getInstance();
      return instance;
    }
 }

public class USTax extends Tax {
     private static USTax instance;
     private USTax() {  
   // instantation local members +  Tax abstract class
 }

 public double calcTax ( double qty, double price){
   // implementation
 }

 public static Tax getInstance() {
     if(instance == null) 
        instance = new USTax();
     return instance;
  }
 }

Old question I know but happened to stumble upon this and think I can add something useful.

It is possible to have a protected constructor in a singleton class. If you want to have polymorphic behavior on your Singleton you can make it an abstract class, set the constructor to protected and delegate creation of the instance to one of the concrete sub classes.

I found the following example in the book "Design Patterns explained":

abstract public class Tax{
    static private Tax instance;

    protected Tax() {};

    abstract double calcTax( double qty, double price);

    public static Tax getInstance() {
      // code to determine what implementing class to use
      instance = USTax.getInstance();
      return instance;
    }
 }

public class USTax extends Tax {
     private static USTax instance;
     private USTax() {  
   // instantation local members +  Tax abstract class
 }

 public double calcTax ( double qty, double price){
   // implementation
 }

 public static Tax getInstance() {
     if(instance == null) 
        instance = new USTax();
     return instance;
  }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文