473,320 Members | 2,124 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 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 2173
"Atul Malaviya" <at***********@gmail.com> a écrit dans le message de news:
11**********************@g43g2000cwa.googlegroups. 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**********************@g43g2000cwa.googlegroups. 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
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: 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
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...
21
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...
0
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...
8
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;...
5
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...
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...
1
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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.