473,325 Members | 2,608 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,325 software developers and data experts.

static vs singlton pattern

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
4 2967
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: lawrence | last post by:
To call I would do something like: $headline = McSelectJustOneField::callDatastore("cbHeadline"); Is this the correct use of the static keyword, to implement a Singleton design?
33
by: Chris Capel | last post by:
What is the rationale behind the decision not to allow abstract static class members? It doesn't seem like it's a logically contradictory concept, or that the implementation would be difficult or...
5
by: Andy | last post by:
Why can't I create an interface and make its implementation static/shared? Is there some pattern that lets me work around this? Any explanation of why this is would be appreciated. This...
17
by: Tom | last post by:
This is not intuitivelly clear.
15
by: ad | last post by:
We can define a static member in a class. But can we define a static class, so that all members are static?
15
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
9
by: Laban | last post by:
Hi, I find myself using static methods more than I probably should, so I am looking for some advice on a better approach. For example, I am writing an app that involves quite a bit of database...
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...
7
by: sherifffruitfly | last post by:
Hi, I'm learning the .net Bloomberg api, and it's main class has all of its stuff static. The help then goes on to say that the class is implemented as a singleton. It's cool I guess to make...
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...
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: 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...
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)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
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
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.