Singleton classes and interfaces
-
Thursday, May 10, 2012 11:34 PM
Hi
I am trying to create an interface and then about 3 or 4 singleton classes that would be accessed via the interface.
I know how to get interfaces to work and I know how to create concrete singleton classes and access them via GetInstance(). But when I put the two together, failure results.
My problem is in the GetInstance method of the concrete singletons as in
Public Class MovingList implements ISelectedList Public Shared Function GetInstance () as MovingList _ Implements ISelectedList.GetInstance() end function end class
ISelectedList is the interface. This fails because ISelectedList.GetInstance is not of the type MovingList. Plus there is a problem with the use of Shared that I think is solved by simply deleting it (interfaces implicitly share).
I can't define the type of ISelectedList.GetInstance because each of my singleton classes is of a different type even though they implement exactly the same properties and methods.
How best to implement such singleton classes? I could do away with the interface and simply deal directly with the concrete singletons but that feels like bad practice. Programming to an abstraction not an implementation and all that.
Any and all help gratefully appreciated.
Doug
VBDataStarter
All Replies
-
Friday, May 11, 2012 5:37 AM
You can't implement interface with shared members. Either you should re-consider using singletons or re-consider using interfaces.
If you want to have different return types for your methods that implement a common interface, make the interface generic:
Public Interface IGenericInterface(Of T) Function Method() As T End Interface Public Class ImplementationA Implements IGenericInterface(Of ImplementationA) Public Function Method() As ImplementationA Implements IGenericInterface(Of ImplementationA).Method End Function End Class Public Class ImplementationB Implements IGenericInterface(Of String) Public Function Method() As String Implements IGenericInterface(Of String).Method End Function End Class Public Class ImplementationC Implements IGenericInterface(Of ImplementationB) Public Function Method() As ImplementationB Implements IGenericInterface(Of ImplementationB).Method End Function End Class
-
Friday, May 11, 2012 9:42 AM
3 or 4 singleton classes ??
This don't sound right to start with. If there is 3 or 4, they are not singleton
What is your goal exactly??
-
Friday, May 11, 2012 2:05 PM
Thanks for this. I see how it works.
So there is no way I can use a singleton class with an interface? I have to program to the concrete singleton class?
VBDataStarter
-
Friday, May 11, 2012 2:22 PM
3 or 4 singleton classes ??
This don't sound right to start with. If there is 3 or 4, they are not singleton
What is your goal exactly??
Thanks Crazypennie
Well they are singletons in that I want one and only instance of the class to exist at any given time. However, I do need each of the different class to exist at the same time.
The reason for making them singletons is that they are data classes that hold different data that is used by my application at many differrent points. Quite often the data will be used by a process, that process will need to be interrupted while another action is completed which changes the data held in one or more of the data classes. Then the original process will continue using the changed data. Given the unpredicatability of these interruptions (perhaps better put as given the high variability of when these interruptions take place and what changes are made to the data), I am very worried about data integrity and the potential for creating multiple, differing versions of the data.
So my thought was to hold specific data in singleton classes so that only one copy could exist and all changes would be made to the same instance of the data.
The same instance of these classes does not persist throughout the life of the application. The singleton classes will constantly be created and destroyed as particular records from the database are used and changed by the application. At the end of the processing, the data in the data classes is written back to the database. I don't want read/write to the database when every change is made as the application requires frequent changing of data, often multiple times within a method.
That's my problem. Perhaps using singleton classes is a bad solution. Other ideas?
VBDataStarter
-
Friday, May 11, 2012 10:57 PM
In that case, you can use an interface to access the different singleton via the interface
Look at this
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim somecondition As Boolean Dim Data As ISelectedList If SomeCondition Then Data = MovingList_1.GetInstance Else Data = MovingList_2.GetInstance End If End Sub End Class Public Interface ISelectedList 'Just put nothing here - Empty interface End Interface Public Class MovingList_1 : Implements ISelectedList Private Shared Instance As MovingList_1 Private Sub New() End Sub Public Shared Function GetInstance() As MovingList_1 If Instance Is Nothing Then Instance = New MovingList_1 Return Instance End Function Public Property data() As String End Class Public Class MovingList_2 : Implements ISelectedList Private Shared Instance As MovingList_2 Private Sub New() End Sub Public Shared Function GetInstance() As MovingList_2 If Instance Is Nothing Then Instance = New MovingList_2 Return Instance End Function Public Property data() As String End Classand you can also use an interface like this if some property or method need to be access
Public Interface ISelectedList Property data() As String End Interface- Edited by CrazypennieMicrosoft Community Contributor Friday, May 11, 2012 11:08 PM
- Marked As Answer by VBdataStarter Monday, May 14, 2012 1:56 AM
-
Monday, May 14, 2012 1:58 AM
Hi Crazypennie
OK. Thanks, I see how this works. If I understand it properly, this would allow me to access methods and properties via the interface, with all the benefits that this brings. It requires me to instantiate the concrete classes directly rather than via the interface. This would certainly work. It feels like a bit of a workaround rather than programming elegance but I guess sometimes that is what is required.VBDataStarter

