Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Singleton pattern or Cache?

Status
Not open for further replies.

steverbs

Programmer
Jul 17, 2003
253
GB
Hi all. I'm a little curious as to how singletons work. In particular, I'm confused as to how they maintain state through multiple requests. If I have a Class named ClassA and it has a Shared method called getClassA, what is the difference between coding the getClassA method in the following two ways:

#1 - Using the application Cache
Friend Shared Function getClassA() As ClassA
If (Not HttpContext.Current.Cache.Item("objClassA") Is Nothing) Then
Return HttpContext.Current.Cache.Item("objClassA")
Else
Dim objClassA As New ClassA
HttpContext.Current.Cache.Item("objClassA") = objClassA
Return objClassA
End If
End Function

#2 - Singleton Pattern
Friend Shared Function getClassA() As ClassA
If isInstantiated = False Then
objClassA = New ClassA
isInstantiated = True
Return objClassA
Else
Return objClassA
End If
End Function

Regards.

Stephen.
 
Hi Stephen,

I think the difference really boils down to where the Singleton'd instance is stored.

Traditionally, it is kept in a static member.

ASP.NET gives us the application cache, which provides the added benefit of expriation and external dependencies.

I just wrote one based on the result of reading in an XML file. I wanted a single instance of the data from the XML file in my application, so I put it in cache. The important thing is that the class that holds that data has a private constructor and a public static GetInstance method, so that in order for any other class to use it, they have to use the singleton accessor. That GetInstance method is what checks the cache for the instance, or creates an new instance (by calling the private constructor) and puts in it cache. Now, using the cache dependency, I can also expect the instance to be removed from cache whenever the XML file is modified, thus giving all the other classes a fresh instance whenever they call the GetInstance accessor.

HTH
 
Cheers for the reply. So does that mean that a singleton in a static member like in the original Gang-of-Four pattern will survive multiple requests?

Steve.
 
Sure. A static member belongs to the class not the instances. This means that each and every instance (over the lifetime of the Application domain) will have access to the same static data.

For a simple test, add a static member to a web page. During the page load, increment the int by one and display it's value. You will see that after each request the number goes up, as long as you do not restart the App domain or the process is recycled.

So it IS tricky with web apps... for this reason I am thinking that it is best to hold only read-only data that can easily be regenerated in a static member.
 
Superb. That has cleared things up for me.

Thanks for the help.

Steve.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top