How to initialize Singleton?

Sometimes it becomes necessary to initialize a singleton class with some auxiliary values. But we cannot "publish" the constructor for it. What is the workaround for this?

Attention! overloading GetInstance or setting color is not my idea. Color should be set only once. I would like to be sure that MyPainter draws ONLY with initialized color. Use any color by default .

For clarity, I provide a sample:

''' <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

Thanks.

+3
source share
6 answers

, , - , -? - , setOptions.

+3

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

, :

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
+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
+1

CreateInstance, . GetInstance. , , , , .

0

Init-: ( #, )

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);
0

Source: https://habr.com/ru/post/1722100/


All Articles