473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Singleton Shanagans

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

Nov 20 '05 #1
3 1377

"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message
news:C0*******************@twister.tampabay.rr.com ...
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


Nov 20 '05 #2
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 Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message
news:C0*******************@twister.tampabay.rr.com ... 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

Nov 20 '05 #3
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

"Jeremy Cowles" <jeremy.cowles[nosp@m]asifl.com> wrote in message
news:Fy*******************@twister.tampabay.rr.com ...
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?


It should go right in the poop collector like everything else.

(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)
Right in the poop collector. =)

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.


Yea, in retrospect, I agree.

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


Most people already wonder what I've been smoking. Now I feel foolish,

but hey, bad ideas are still ideas right?

=)

~
Jeremy

"Stop thinking, and end your problems."
-Lao Tzu

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a)...
10
by: E. Robert Tisdale | last post by:
Could somebody please help me with the definition of a singleton? > cat singleton.cc class { private: // representation int A; int B; public: //functions
1
by: Jim Strathmeyer | last post by:
So I'm trying to implement a singleton template class, but I'm getting a confusing 'undefined reference' when it tries to link. Here's the code and g++'s output. Any help? // singleton.h ...
3
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic...
7
by: Ethan | last post by:
Hi, I have a class defined as a "Singleton" (Design Pattern). The codes are attached below. My questions are: 1. Does it has mem leak? If no, when did the destructor called? If yes, how can I...
3
by: Harry | last post by:
Hi ppl I have a doubt on singleton class. I am writing a program below class singleton { private: singleton(){}; public: //way 1
5
by: Pelle Beckman | last post by:
Hi, I've done some progress in writing a rather simple singleton template. However, I need a smart way to pass constructor arguments via the template. I've been suggested reading "Modern C++...
3
weaknessforcats
by: weaknessforcats | last post by:
Design Pattern: The Singleton Overview Use the Singleton Design Pattern when you want to have only one instance of a class. This single instance must have a single global point of access. That...
3
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.