有时需要使用一些辅助值初始化单例类。但我们不能“发布”它的构造函数。解决方法是什么?
注意! 重载GetInstance或设置颜色不是我的想法。颜色应仅设置一次。我想确保MyPainter使用初始化颜色绘制 ONLY 。 应该使用任何默认颜色。
为了更加清晰,我提供了一个示例:
''' <summary>
''' Singleton class MyPainter
''' </summary>
Public Class MyPainter
Private Shared _pen As Pen
Private Shared _instance As MyPainter = Nothing
Private Sub New()
End Sub
''' <summary>
''' This method should be called only once, like a constructor!
''' </summary>
Public Shared Sub InitializeMyPainter(ByVal defaultPenColor As Color)
_pen = New Pen(defaultPenColor)
End Sub
Public Shared Function GetInstance() As MyPainter
If _instance Is Nothing Then
_instance = New MyPainter
End If
Return _instance
End Function
Public Sub DrawLine(ByVal g As Graphics, ByVal pointA As Point, ByVal pointB As Point)
g.DrawLine(_pen, pointA, pointB)
End Sub
End Class
感谢。
答案 0 :(得分:3)
如果你想在创建时只初始化一次,为什么不在构造函数中通过调用某个方法来完成它,这会从某个地方提取参数?如果多次调用此初始化 - 将其转换为单独的方法,如setOptions。
答案 1 :(得分:2)
你能不能只重载你的GetInstance方法吗?
Dim _IsInitialized as boolean = false
Public Shared ReadOnly Property IsInitialized() As Boolean
Get
Return _IsInitialized
End Get
End Property
Public Shared Function GetInstance() As MyPainter
If _instance Is Nothing Then
_instance = New MyPainter
End If
_IsInitialized = True
Return _instance
End Function
Public Overloads Shared Function GetInstance(ByVal DefaultPenColor As Color) As MyPainter
If _instance Is Nothing Then
_instance = New MyPainter
InitializeMyPainter(DefaultPenColor)
End If
_IsInitialized = True
Return _instance
End Function
在调用GetInstance方法之前,请检查它是否已初始化。如果没有给它笔颜色。
If MyPainter.IsInitialized then
Dim Painter as MyPainter = MyPainter.GetInstance
Else
Dim Painter as MyPainter - MyPainter.GetInstance(System.Drawing.Color.Red)
EndIf
答案 2 :(得分:1)
您可以声明共享构造函数,但它们必须是无参数的:
Public Class MyPainter
Private Shared _pen As Pen
Shared Sub New()
_pen = New Pen(Color.White)
End Sub
....
End Class
您可以在构造函数中将颜色设置为合理的默认值,然后添加辅助方法以根据需要更改颜色,就像您在样本中一样:
Public Shared Sub SetColor(ByVal defaultPenColor As Color)
_pen = New Pen(defaultPenColor)
End Sub
答案 3 :(得分:1)
您可以在GetInstance中使用可选参数
Public Shared Function GetInstance(Optional ByVal defaultPenColor As Color = Colors.Red) As MyPainter
If _instance Is Nothing Then
If defaultPenColor is nothing then
defaultPenColor = Colors.Red
end if
_pen = New Pen(defaultPenColor)
_instance = New MyPainter
End If
Return _instance
End Function
答案 4 :(得分:0)
创建一个采用所需参数的CreateInstance
方法。并从GetInstance
中删除创建功能。那不完全是单身,但至少你知道事情会被正确地初始化。
答案 5 :(得分:0)
你可以使用一个Init方法:(这是一个C#的例子,我刚刚遇到了与可初始化的单例相同的问题)
public sealed class XSingleton
{
private static XSingleton instance = null;
int value = -1;
private XSingleton(int v)
{
value = v;
}
public static void Init(int v)
{
if (instance != null) throw new Exception("Init() must be called only once!");
instance = new XSingleton(v);
}
public static XSingleton Instance
{
get
{
if (instance == null) throw new Exception("Call Init() before!");
return instance;
}
}
public int Value { get { return value; } }
}
如果您尝试使用XSingleton unitinialized或尝试初始化多次,则会出现异常。
所以你用
初始化单例XSingleton.Init(123);
现在您可以使用已初始化的值:
Console.WriteLine(XSingleton.Instance.Value);