473,796 Members | 2,712 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2994
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
3222
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
3359
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 near-impossible. It seems like it would be useful. In fact, there's a place in my code that I could make good use of it. So why not? Chris
5
3970
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 is the message I get for the following code:
17
51470
by: Tom | last post by:
This is not intuitivelly clear.
15
3345
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
64132
by: DBA | last post by:
Hi All, What is the diff. between a singleton class and a static class in C#?
9
2665
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 operations on purchase orders and inventory. I have created a PurchaseOrder class and Inventory class to encapsulate operations like creating POs, finding items, etc. These two classes are used extensively from different parts of the app.
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:
7
1463
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 sure not more than one instance can be made, but with everything in the class static, I don't actually understand why even *one* instance is required. What's the design rationale for this?
0
9673
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
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10168
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
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9047
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5440
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
5568
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4114
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
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.