我应该为我的单例类创建受保护的构造函数吗?
根据设计,在单例模式中,构造函数应标记为私有,并提供一个创建方法来重新调整同一类型实例的私有静态成员。我只像这样创建了我的单例类。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您通过继承扩展单例类,那么如果有人获取您的单例和原始单例,您将有 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?
如果你这样做,它就不是单身人士。但也许你并不真正需要单身人士。
If you do that, it's not a singleton. But perhaps you don't really need a singleton.
这不是单例类。想象一下,我可以调用 getInstance() 静态方法 n 次,并且我可以拥有该类的 n 个对象,从而完全违反了单例模式。要使其成为单例,您应该在 getInstance() 方法中检查对象是否已创建。如果已经创建,那么您应该忽略并且不要再次创建。例如,你可以这样类似的东西,请忽略语法错误,只是一个代码来解释,在不同的语言中可能会有所不同。
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.
我知道的老问题,但碰巧发现了这个,我想我可以添加一些有用的东西。
单例类中可以有受保护的构造函数。如果您想在 Singleton 上具有多态行为,您可以将其设为抽象类,将构造函数设置为受保护,并将实例的创建委托给具体子类之一。
我在《设计模式详解》一书中找到了以下例子:
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":