473,799 Members | 3,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton pattern vs Class with purely static members

>From a design/usability perspective.

When will one use a singleton pattern and when a class with purely
static members?

What are the pros and cons? I have inherited a code base which is full
of both these and I am a bit confused on this count.

Singleton insures one object of a class in the application. A class
with purely static members and a private constructor also strives to
achieve the same.

The only difference I can think of is that in the second case we are
not creating an object so we are saving some memory space. Which,in my
opinion is not a decent enough reason to decide against one of the
options.

Where will we use one and not the other? Any inputs/insights will be
appreciated.

Nov 17 '05 #1
7 2191
"Atul Malaviya" <at***********@ gmail.com> a écrit dans le message de news:
11************* *********@g43g2 00...legr oups.com...
When will one use a singleton pattern and when a class with purely
static members?


I find that the only time I would use a "created" Singleton as opposed to a
static class, is when resources that are expensive need to be released as
soon as they are finished with whereas, with a static class, any state
fields are kept in memory until the program quits.

Joanna

--
Joanna Carter
Consultant Software Engineer
Nov 17 '05 #2
Thanks for the prompt reply Joanna.
That pretty much clears the air and gives me a defining line.

Nov 17 '05 #3
Joanna Carter (TeamB) wrote:
"Atul Malaviya" <at***********@ gmail.com> a écrit dans le message de
news: 11************* *********@g43g2 00...legr oups.com...
When will one use a singleton pattern and when a class with purely
static members?


I find that the only time I would use a "created" Singleton as
opposed to a static class, is when resources that are expensive need
to be released as soon as they are finished with whereas, with a
static class, any state fields are kept in memory until the program
quits.

Joanna


For an asp.net application, I use it precisely the other way around:
Singleton for things I want to keep in memory (application wide), such
as configuration data
Static methods (*without* using static members) if I want to clean up
immediately.

If I understand correctly, a singleton is created once "somewhere"
in the lifetime of the application and usually never destroyed, until the
application ends. The data it contains might be refreshed.
Hans Kesting
Nov 17 '05 #4
Hans Kesting said:
"Static methods (*without* using static members) if I want to clean up
immediately."

What will an static method cleanup then? Because it can access only
static members. Can you please explain it a bit more?

In case of singleton pattern if the object has been Garbage collected
then a new one will be created and passed back(sort of refreshing the
data). The same can be achieved in case of purely static member class
by implementing a static method which will clean up the required static
members.

Nov 17 '05 #5
Atul Malaviya wrote:
Hans Kesting said:
"Static methods (*without* using static members) if I want to clean up
immediately."

What will an static method cleanup then? Because it can access only
static members. Can you please explain it a bit more?

"only static members", it can only access static members of *it's own class*
(as there is no instance)
There is no-one preventing you from instantiating local variables, which
might require cleanup.
In case of singleton pattern if the object has been Garbage collected
then a new one will be created and passed back(sort of refreshing the
data). The same can be achieved in case of purely static member class
by implementing a static method which will clean up the required
static members.


A singleton should never be GC'ed, as there is a reference to it (it holds
a static reference to itself)

Hans Kesting
Nov 17 '05 #6
Hi,


I find that the only time I would use a "created" Singleton as opposed to
a
static class, is when resources that are expensive need to be released as
soon as they are finished with whereas, with a static class, any state
fields are kept in memory until the program quits.


No necessarily, in both cases you can create methods to dispose the
resources as needed.
IMO the main difference is that with a singleton pattern the code using it
has no knowledge that a single instance exist. the code treat the instance
as any other instance. The "singleton" part is transparent. If the developer
later decide to have more than one instance , let's say one instance per
processor just to put an example, the calling code need NO recompiling nor
change.

In the case of a static class the calling code call the methods using the
type , not an instance so if any change happen the calling code needs to be
modified and recompiled.

Those are subtle but important differences !

I use static (either classes or member ) when there is clear that it does
not form part of an instance. The best example is the Math class.

I use a singleton wherever an instance is needed or is what makes sense.
HTH,
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 17 '05 #7
Hi,

For an asp.net application, I use it precisely the other way around:
Singleton for things I want to keep in memory (application wide), such
as configuration data
Static methods (*without* using static members) if I want to clean up
immediately.
They are called stateless , you can get the same with any type, you do not
need to be static, but this is a VERY good place where to use static, if you
do not need to keep a state then your method may have more context in a type
wide, than in an instance.
If I understand correctly, a singleton is created once "somewhere"
in the lifetime of the application and usually never destroyed, until the
application ends. The data it contains might be refreshed.


No really you can use either approach and create a method to get the
internal state to its original one, with this you refresh your instance/type
as needed. read my other post for the real difference between them.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Nov 17 '05 #8

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

Similar topics

7
12480
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) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
10
5312
by: ferdinand.stefanus | last post by:
Hi Could someone tell me what's the difference between these two singleton implementations: First implementation: class Singleton { public:
11
2172
by: Daniel Billingsley | last post by:
Let's say I'm writing a business app and I want there to be only one instance of the Customer object for each particular customer (representing a database record) being edited. Would it be possible to extend the Singleton pattern to handle this? Assuming that my Customer class follows the Singleton pattern (particularly Skeet's 4th version) I'm thinking if I add private static SomeCollectionType customers;
21
2471
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this...
0
1727
by: Tony Wong | last post by:
I am trying to implement the Singleton Pattern for a assembly (class) which control a common resource on my computer. I need the Singleton behavior within a single process which contain multiple AppDomains. I use a static member to count class instance like this, public __gc class foo: public IDisposable { public:
8
2823
by: Gaensh | last post by:
HI, I have a singleton pattern to acess my database the following is the sample code use to implement singleton pattern public class DataAccessHelper { private static DataAccessHelper instance; /// <summary> /// public property that can only get the single instance of this class. /// </summary>
5
1610
by: Diffident | last post by:
Hello All, I am designing a class based on singleton pattern. Inside this class I have multiple instance methods. My question is since there will be only one instance of this class at any instance of time in the whole application there is no use in having these methods as instance methods I can as well have static methods....correct? If I leave these methods as instance methods would they have any wrong impact on my application?
3
18255
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 is, regardless of where the object is hidden, everyone needs access to it. The global point of access is the object's Instance() method. Individual users need to be prevented from creating their own instances of the Singleton.
1
1502
by: =?Utf-8?B?Qi4gQ2hlcm5pY2s=?= | last post by:
I'm getting a little confused here. I have a C# class that I'm trying to translate to VB. The C# class is essentially: public static class Class1 { ..... some private static variables and public static properties. } The translation (from several different translation programs) is:
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10219
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.