Singleton Shanagans

  • Thread starter Thread starter Jeremy Cowles
  • Start date Start date
J

Jeremy Cowles

Will the keyword "Me" ever be able to be the target of an assignment?
Because singleton classes would be MUCH simplier if you could just point Me
to a different address. For example:

Class SingletonClassic
Private Shared sm_Instance as SingletonClassic

Public Shared Sub GetInstance()
Return sm_Instance
End Sub
End Class

Class SingletonNew
Private Shared sm_Instance as SingletonClassic

Sub New
If not sm_Instance is Nothing then
me = sm_instance
return
End If
End Sub
End Class

Usage (classic):

Dim sc as SingletonClassic
sc = sc.GetInstance( )

Usage (new):

Dim sn as New SingletonNew( )


isnt the second much nicer? You could totally hide the fact that your class
only has one instance...

~
Jeremy
 
Jeremy Cowles said:
Will the keyword "Me" ever be able to be the target of an assignment?
Because singleton classes would be MUCH simplier if you could just point Me
to a different address. For example:

Class SingletonClassic
Private Shared sm_Instance as SingletonClassic

Public Shared Sub GetInstance()
Return sm_Instance
End Sub
End Class

Class SingletonNew
Private Shared sm_Instance as SingletonClassic

Sub New
If not sm_Instance is Nothing then
me = sm_instance
return
End If
End Sub
End Class

Usage (classic):

Dim sc as SingletonClassic
sc = sc.GetInstance( )

Usage (new):

Dim sn as New SingletonNew( )


isnt the second much nicer? You could totally hide the fact that your class
only has one instance...

No. It's completely confusing. You would be lying to the class user,
making it appear as if you could have two different instances of your
singleton type.


BTW, I think this form of the singleton is nicer:

Class SingletonClassic
Public Shared ReadOnly instance as new SingletonClassic()
Private sub New()
'initialize
end sub

End Class

Then from code you don't do this

Dim sc as SingletonClassic
sc = sc.instance
sc.doSomething()

You just do

SingletonClassic.instance.doSomething()

David
 
Jeremy,
Will the keyword "Me" ever be able to be the target of an assignment?
No. The problem is that your constructor does not return any memory, the
memory is already allocated by the time the constructor is called. Also, if
you allow Me to be assigned to, what happens to the base object that has
already been partially constructed when a derived class assigns to Me?
(remember all classes derive from Object) Especially where the base
constructors may have documented side effects? (the base is intended to be a
singleton, but you are creating 'partial instances' that are being
discarded)

Operator New does 3 things.
1. Allocates a block of memory for the object
2. Calls the corresponding constructor for the object using this block of
memory. Which calls base constructors.
3. Returns the above allocated block of memory.
Dim sn as New SingletonNew( )
isnt the second much nicer?
I have not used remoting much, but I understand this is what happens in
remoting...

As David stated its "completely" confusing.
You could totally hide the fact that your class
only has one instance...
If using Operator New is important to you for singleton, then consider
setting up a proxy class. Of course developers who understand patterns, may
wonder what you've been smoking ;-)

Your Proxy class allows creation of a new object, while internally this
object refers to a true singleton.

Something like (numerous other variations possible):
Class SingletonNew
Private Shared sm_Instance as SingletonClassic
Private Class SingletonClassic
' Seeing as this is private SingletonNew
' controls everything
End Class
Sub New
If sm_Instance is Nothing then
sm_instance = New SingletonClassic
End If
End Sub

' Then each method delegates to the real method in SingletonClassic
Public Sub Method1()
sm_Instance.Method1()
End Sub
End Class

Or instead of instance fields, only create shared fields.

I don't think I would actually use either of these, but they are options.

If inheritance is not important for your singleton, then consider making a
NotInheritable class where all the methods are Shared and a private
constructor.

Hope this helps
Jay
 
Jeremy,
It should go right in the poop collector like everything else.
I sure it will! Just like what happens when your constructor throws an
exception.

That wasn't really the point.

I'm suggesting the base constructor changed some value, that because the
base is intended to be a singleton it is O.K. to change ONCE! however
because you are calling it repeatedly this value is being changed far too
often... (for example it clears a log file, clearing it the 'only' time it
is created is fine, however if you are continually calling the base
constructor, which is continually clearing the log file...) Sure there are
work around, but do we really need workarounds, when the base was intended
to be a singleton any way?
Most people already wonder what I've been smoking. Now I feel foolish, but
hey, bad ideas are still ideas right?
I wouldn't feel too foolish, it was actually a good question. When going
across AppDomains (and machines) the basis of your idea makes sense. An
actual object needs to be created in the one 'domain', while a singleton
exists in the other. I don't do a lot of Remoting, I believe this is
effectively what Remoting does, only rather than change Me, it changes an
instance variable...

Remember the only foolish question is the one not asked.

Hope this helps
Jay
 
Back
Top