473,881 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Public Static Variables vs. the Singleton Class

I've just been reading all about the Singleton class and understand how to
implement and use it but I cannot understand why one NEEDS to use it instead
of just declaring a class and implementing Public Static variables therein.

My usage revolves around global access to Reference Data. Some examples:
- Countries, States-Provinces, Cities, Area Codes
- Mobile Devices: Name, Description, Picture Filename

and other sorts of data that will rarely, if ever change. And since there
will always only need to be one copy of the data, going the Public Static
Variable route seems like the logical route.

Or am I missing something?

Robert W.
Vancouver, BC

Nov 17 '05 #1
7 2570
The big drawbacks of static classes is that you can never treat them
like objects, and they don't participate in the class hierarchy and
thus don't support polymorphism.

The first drawback means that if you make a static class, you can never
pass it as a whole to any method, or store a reference to it in a
variable. Now, there are classes for which that's clearly not needed,
but it's a design consideration: "Will I never need to pass this thing
as an argument?"

The second drawback has more to do with future considerations. Would it
make any sense to have different versions of this thing for different
environments? Even if it stores no state, it may embody the
implementation of algorithms, and you may want to be able to "plug in"
different algorithms at a later date (Strategy pattern). Database
access layers are clear examples of things that look like they could be
static but, on further examination, shouldn't be, because a static
implementation precludes the support of multiple data sources, or at
least makes it more difficult.

If you're sure that you're never going to have to treat the thing as an
object (pass it to methods, etc), and you're sure that it will never
need to participate in the class hierarchy, then a static class is
fine.

One example you gave is a good one: if the class is just a glorified
constant-holder (countries, provinces, states, etc) then I see no need
to make a singleton of it.

Nov 17 '05 #2
Bruce,

Thanks for your feedback! As a newbie to C# and OOP I've still got a lot to
learn. You make some good points that I hadn't considered, especially the
part about passing the class to a method. I can already envisage writing a
method that would take the Reference Data object and iterate through it to
perform various tests for integrity, spelling, and such. While I could just
access the data in a non-OOP way, I'm trying to do things correctly from the
get go.

Thanks again!

Robert W.


"Bruce Wood" wrote:
The big drawbacks of static classes is that you can never treat them
like objects, and they don't participate in the class hierarchy and
thus don't support polymorphism.

The first drawback means that if you make a static class, you can never
pass it as a whole to any method, or store a reference to it in a
variable. Now, there are classes for which that's clearly not needed,
but it's a design consideration: "Will I never need to pass this thing
as an argument?"

The second drawback has more to do with future considerations. Would it
make any sense to have different versions of this thing for different
environments? Even if it stores no state, it may embody the
implementation of algorithms, and you may want to be able to "plug in"
different algorithms at a later date (Strategy pattern). Database
access layers are clear examples of things that look like they could be
static but, on further examination, shouldn't be, because a static
implementation precludes the support of multiple data sources, or at
least makes it more difficult.

If you're sure that you're never going to have to treat the thing as an
object (pass it to methods, etc), and you're sure that it will never
need to participate in the class hierarchy, then a static class is
fine.

One example you gave is a good one: if the class is just a glorified
constant-holder (countries, provinces, states, etc) then I see no need
to make a singleton of it.

Nov 17 '05 #3
Incidentally, could I ask one more question? After implementing the
Singleton class, I instantiated it (the single copy) like this:

static void Main()
{
Singleton test = Singleton.Insta nce;

Application.Run (new frmMain());
}
This all works fine but I then discovered that I could not access "test"
from anywhere else in the application. Instead I had to use
"Singleton.Inst ance." as a prefix before the property or variable.

Is this indeed correct that I'm not supposed to able to access "test" from
elsewhere in the application?

Robert


"Robert W." wrote:
Bruce,

Thanks for your feedback! As a newbie to C# and OOP I've still got a lot to
learn. You make some good points that I hadn't considered, especially the
part about passing the class to a method. I can already envisage writing a
method that would take the Reference Data object and iterate through it to
perform various tests for integrity, spelling, and such. While I could just
access the data in a non-OOP way, I'm trying to do things correctly from the
get go.

Thanks again!

Robert W.


"Bruce Wood" wrote:
The big drawbacks of static classes is that you can never treat them
like objects, and they don't participate in the class hierarchy and
thus don't support polymorphism.

The first drawback means that if you make a static class, you can never
pass it as a whole to any method, or store a reference to it in a
variable. Now, there are classes for which that's clearly not needed,
but it's a design consideration: "Will I never need to pass this thing
as an argument?"

The second drawback has more to do with future considerations. Would it
make any sense to have different versions of this thing for different
environments? Even if it stores no state, it may embody the
implementation of algorithms, and you may want to be able to "plug in"
different algorithms at a later date (Strategy pattern). Database
access layers are clear examples of things that look like they could be
static but, on further examination, shouldn't be, because a static
implementation precludes the support of multiple data sources, or at
least makes it more difficult.

If you're sure that you're never going to have to treat the thing as an
object (pass it to methods, etc), and you're sure that it will never
need to participate in the class hierarchy, then a static class is
fine.

One example you gave is a good one: if the class is just a glorified
constant-holder (countries, provinces, states, etc) then I see no need
to make a singleton of it.

Nov 17 '05 #4
Of course not. The variable "test" only has a scope for the Main()
function.

Correct me if I'm wrong, but the whole point of a singleton is actually use
the "Singleton.Inst ance.<some_prop erty_or_method> syntax in code.

"Robert W." <Ro*****@discus sions.microsoft .com> wrote in message
news:A6******** *************** ***********@mic rosoft.com...
Incidentally, could I ask one more question? After implementing the
Singleton class, I instantiated it (the single copy) like this:

static void Main()
{
Singleton test = Singleton.Insta nce;

Application.Run (new frmMain());
}
This all works fine but I then discovered that I could not access "test"
from anywhere else in the application. Instead I had to use
"Singleton.Inst ance." as a prefix before the property or variable.

Is this indeed correct that I'm not supposed to able to access "test" from
elsewhere in the application?

Robert


"Robert W." wrote:
Bruce,

Thanks for your feedback! As a newbie to C# and OOP I've still got a lot
to
learn. You make some good points that I hadn't considered, especially
the
part about passing the class to a method. I can already envisage writing
a
method that would take the Reference Data object and iterate through it
to
perform various tests for integrity, spelling, and such. While I could
just
access the data in a non-OOP way, I'm trying to do things correctly from
the
get go.

Thanks again!

Robert W.


"Bruce Wood" wrote:
> The big drawbacks of static classes is that you can never treat them
> like objects, and they don't participate in the class hierarchy and
> thus don't support polymorphism.
>
> The first drawback means that if you make a static class, you can never
> pass it as a whole to any method, or store a reference to it in a
> variable. Now, there are classes for which that's clearly not needed,
> but it's a design consideration: "Will I never need to pass this thing
> as an argument?"
>
> The second drawback has more to do with future considerations. Would it
> make any sense to have different versions of this thing for different
> environments? Even if it stores no state, it may embody the
> implementation of algorithms, and you may want to be able to "plug in"
> different algorithms at a later date (Strategy pattern). Database
> access layers are clear examples of things that look like they could be
> static but, on further examination, shouldn't be, because a static
> implementation precludes the support of multiple data sources, or at
> least makes it more difficult.
>
> If you're sure that you're never going to have to treat the thing as an
> object (pass it to methods, etc), and you're sure that it will never
> need to participate in the class hierarchy, then a static class is
> fine.
>
> One example you gave is a good one: if the class is just a glorified
> constant-holder (countries, provinces, states, etc) then I see no need
> to make a singleton of it.
>
>

Nov 17 '05 #5
Robert W. wrote:
Incidentally, could I ask one more question? After implementing the
Singleton class, I instantiated it (the single copy) like this:

static void Main()
{
Singleton test = Singleton.Insta nce;

Application.Run (new frmMain());
}
This all works fine but I then discovered that I could not access "test"
from anywhere else in the application. Instead I had to use
"Singleton.Inst ance." as a prefix before the property or variable.

Is this indeed correct that I'm not supposed to able to access "test" from
elsewhere in the application?

Robert

This is indeed correct.
You create a reference to your singleton instance object but that
reference will die once it passes out of scope. (as soon as Main finishes)

With singletons, you still need to declare a reference to your instance,
its just that these references will always point to the same instance.
(As compared to non-singleton which will point to different instances
(mostly))

You could access it via Singleton.Insta nce.<property/method/etc..>

HTH

JB
Nov 17 '05 #6
Yes. In fact, you don't need to instantiate the singleton in your Main
method. Just let the first access to Singleton.Insta nce create it. It
will be transparent to calling code.

After the first reference to Singleton.Insta nce, all future references
will just return the already-created object, so they cost hardly
anything.

And yes, it's the common idiom to see Singleton.Insta nce everywhere in
your code that you need the singleton, although it's also perfectly
reasonable to pop it into a local variable if you need to use it
repeatedly within a local bit of code.

Nov 17 '05 #7
Yes. In fact, you don't need to instantiate the singleton in your Main
method. Just let the first access to Singleton.Insta nce create it. It
will be transparent to calling code.

After the first reference to Singleton.Insta nce, all future references
will just return the already-created object, so they cost hardly
anything.

And yes, it's the common idiom to see Singleton.Insta nce everywhere in
your code that you need the singleton, although it's also perfectly
reasonable to pop it into a local variable if you need to use it
repeatedly within a local bit of code.

Nov 17 '05 #8

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

Similar topics

9
2318
by: Tim Clacy | last post by:
Would some kind soul suggest a pre-processor test for the C++ language revision whereby class static variables were specified to refer to the same instance? Specifically, the following Singleton template will work with some compilers but not with older ones (because every module that includes the header gets its own unique static 'instance'): template<typename T> struct Singleton { static T& Instance() { static T instance; return...
4
6188
by: baumann | last post by:
hi all, according the private / protected access control, - private; that is, its name can be used only by members and friends of the class in which it is declared. - protected; that is, its name can be used only by members and friends of the class in which it is
5
5622
by: Jake | last post by:
I'm just getting into design patterns and am not sure I fully understand the usefulness of the singleton. I know you can use it to ensure that you only have one instance of a class, but why would you do that rather then just using static methods on your class. Any input would be great. Thank you, Jake
5
1633
by: Tee | last post by:
Hi, In VB, we can add a module to put all the variables and functions, do we have something similar in C#? Thanks
27
2732
by: thomasp | last post by:
Variables that I would like to make available to all forms and modules in my program, where should I declare them? At the momment I just created a module and have them all declared public there. What is the normal way to do this? Thanks, Thomas --
8
7516
by: Bill Cohagan | last post by:
I'm curious as to why C# doesn't support static indexers. Anybody know? Thanks, Bill
7
4235
by: John A Grandy | last post by:
For a singleton class utilizes by ASP.NET 2.0 page processing: When initial instantiation is performed during the initial call to the retrieve instance method (let's call the method "getInstance()"), an instantiated object of the class is assigned to the class' internal static reference to that an object of itself (let's call the reference "uniqueInstance") Does that uniqueInstance have global scope in the context of page processing ?
2
3033
by: Darrel | last post by:
I'm working on an app where the ASPX pages aren't precompiled with the class.vb files I'm. This is so people can add their own ASPX pages down the road to the app (the .aspx pages become 'templates' in a way). As such, each template has a function call at the top: <% retrieveData() %> and then within the page, wherever they want to show some text from the db, they just use one of the variables sent in the vb file:
15
3007
by: esha | last post by:
I need to have a Public variable in my project. In VB it can be declared in a standard module. Where can I do it in C# ? I tried to do it in default class Program.cs and I tried it in an added by me class. No success So, how and where I declare a variable visible by any module in the project? Esha
0
9926
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
10716
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
10812
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,...
1
7952
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7108
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4597
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
4194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3223
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.