By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,804 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

static vs singlton pattern

P: n/a
I have a class with bunch of static methods. I could regourp all the
static methods into seperate 3 or 4 classes. I was thinking about using
Singlton pattern for all these 4 classes so that it behaves like static
still class. But my concern is, when going from static to singlton, I
need to add one more line to make sure the class is instantiated.

Am I going in right direction?
Thanks for the help.

Sep 11 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
DBC User wrote:
I have a class with bunch of static methods. I could regourp all the
static methods into seperate 3 or 4 classes. I was thinking about using
Singlton pattern for all these 4 classes so that it behaves like static
still class. But my concern is, when going from static to singlton, I
need to add one more line to make sure the class is instantiated.

Am I going in right direction?
If the class contains static data, then I think moving
to Singleton is the right direction.

If the class only contains methods, then Singleton
does not make sense.

Arne
Sep 11 '06 #2

P: n/a
As Arne said, if the class contains only methods then making it a
singleton may not make sense.

Making a class a Singleton (or something close to one) really does only
one thing for you: allows your class to exhibit polymorphic behaviour,
which a static class cannot do.

This means that you can have your class implement interfaces, have it
participate in the inheritance hierarchy by having child classes
(although then it's not technically a singleton any more, but this can
be useful nonetheless), etc. These things mean that you can pass your
class to methods that accept classes that implement those interfaces
(which you can't do with a static class).

There are cases in which classes with no state can make use of
polymorphism, but I'm hard pressed to think of one in which a
_singleton_ class with no state can benefit from polymorphism.

As an example of a "singleton" that I wrote, I have data layer classes
that are singletons for each data table in the database: each data
table corresponds to exactly one data layer object. However, they all
inherit from DataHandler, which contains some generic code, and allows
me to write methods that accept and work with any DataHandler.
Technically speaking, these aren't singletons, but they're close enough
for me. I couldn't do this with static classes, because static classes
don't meaningfully participate in the class hierarchy.

However, for an example of a static class that has no business being a
singleton (and therefore isn't), take a look at System.Math in the .NET
Framework. It's just a collection of methods and constants, with no
state.

Arne Vajhøj wrote:
DBC User wrote:
I have a class with bunch of static methods. I could regourp all the
static methods into seperate 3 or 4 classes. I was thinking about using
Singlton pattern for all these 4 classes so that it behaves like static
still class. But my concern is, when going from static to singlton, I
need to add one more line to make sure the class is instantiated.
>
Am I going in right direction?

If the class contains static data, then I think moving
to Singleton is the right direction.

If the class only contains methods, then Singleton
does not make sense.

Arne
Sep 11 '06 #3

P: n/a
Thanks, yes it has data. But my question is does it mean, instead of
one line that I use to reference now I need two lines, one for
inistansiation and anohter one is the method call??
Thanks.
Arne Vajhøj wrote:
DBC User wrote:
I have a class with bunch of static methods. I could regourp all the
static methods into seperate 3 or 4 classes. I was thinking about using
Singlton pattern for all these 4 classes so that it behaves like static
still class. But my concern is, when going from static to singlton, I
need to add one more line to make sure the class is instantiated.
>
Am I going in right direction?

If the class contains static data, then I think moving
to Singleton is the right direction.

If the class only contains methods, then Singleton
does not make sense.

Arne
Sep 11 '06 #4

P: n/a
DBC User wrote:
Thanks, yes it has data. But my question is does it mean, instead of
one line that I use to reference now I need two lines, one for
inistansiation and anohter one is the method call??
Instead of:

C.m();

you call:

C.Instance.m();

Arne
Sep 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.