Question Implementing Singleton in Parent Class

Maverick

New member
Joined
Dec 18, 2006
Messages
1
Programming Experience
3-5
Hi Everyone,
Can anyone please guide me to implement singleton in a parent class.
Infact what I want is that All the childs etc. should use same instance of the parent.

I have implemented singleton, Changed the scope of parent class's constructor to "Protected". But in the application when ever the new child's constructor is called i.e. "Dim abc as New Ch1()" the constructor of Parent is called (as it is Proctected and can be access from child). Which is undesired behaviour.

Can anyone please give me some sample code etc for this or atleast some idea that how to implement singleton in parent class.


Thanks & Regards,
Maverick
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,171
Location
Sydney, Australia
Programming Experience
10+
Your design doesn't make sense. If there's only supposed to be a single instance of the base class, how can there be a single instance each of multiple derived classes? Each instance of a derived class is also an instance of the base class, so that means that there is more than a single instance of the base class. It simply doesn't make sens eto inherit a singleton class.
 

mbb

Well-known member
Joined
Jun 3, 2009
Messages
52
Programming Experience
10+
Hi Everyone,
Can anyone please guide me to implement singleton in a parent class.
Infact what I want is that All the childs etc. should use same instance of the parent.

I have implemented singleton, Changed the scope of parent class's constructor to "Protected". But in the application when ever the new child's constructor is called i.e. "Dim abc as New Ch1()" the constructor of Parent is called (as it is Proctected and can be access from child). Which is undesired behaviour.

Can anyone please give me some sample code etc for this or atleast some idea that how to implement singleton in parent class.


Thanks & Regards,
Maverick
Your understanding of what is a singleton doesn't seem right to me.

If you have a parent object that instantiates multiple children objects, and the children have to reference the parent, then pass the parent's object into the childs constructor. Something like below where your application's main form is:

Code:
public Class ParentClass
    Public Sub New()
    End Sub

    Public Sub MakeChild()
        Dim child as ChildClass = new ChildClass(me)
    End Sub
End class

public Class ChildClass

    Private _Parent as ParentClass = nothing

    public sub New(pParent as ParentClass)
        _Parent = pParent

        ' Do other stuff here
    end sub
end class

public Class frmMain
  Private anObject as ParentClass = new ParentClass()

  public sub FormLoad()
      ' Make a children objects
      anObject.MakeChild()
      anObject.MakeChild()
      anOjbect.MakeChild()

      ' Or
      Private childObject1 as ChildObject = new ChildObject(anObject)
      Private childObject2 as ChildObject = new ChildObject(anObject)
      Private childObject3 as ChildObject = new ChildObject(anObject)
      Private childObject4 as ChildObject = new ChildObject(anObject)
  end sub

end class
An example of a singleton implementation is this:

Code:
Public Class Index

    Private Shared ReadOnly LOCK As New Object()
    Private Shared _Instance As Index = Nothing

    Shared ReadOnly Property Instance() As Index
        Get
            If _Instance Is Nothing Then
                SyncLock LOCK
                    If _Instance Is Nothing Then
                        _Instance = New Index()
                    End If
                End SyncLock
            End If

            Return _Instance
        End Get
    End Property

    Private Sub New()
    End Sub
End Class
If your parent was truely singleton then you should be able to do something like below. Here I don't have to pass in the parent reference to the ChildClass because it can access it for itself from the singleton instance property:

Code:
public Class ParentClass
    ' Singleton implementation
    ' .........
End class

public Class ChildClass

    Private _Parent as ParentClass = ParentClass.Instance

    public sub New()
        ' Do other stuff here
    end sub
end class

public Class frmMain
  Private anObject as ParentClass = new ParentClass()

  public sub FormLoad()

      Private childObject1 as ChildObject = new ChildObject()
      Private childObject2 as ChildObject = new ChildObject()
      Private childObject3 as ChildObject = new ChildObject()
      Private childObject4 as ChildObject = new ChildObject()
     end sub

end class
 
Top Bottom