473,385 Members | 1,311 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,385 software developers and data experts.

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 2543
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.Instance;

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.Instance." 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.Instance.<some_property_or_method> syntax in code.

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:A6**********************************@microsof t.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.Instance;

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.Instance." 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.Instance;

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.Instance." 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.Instance.<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.Instance create it. It
will be transparent to calling code.

After the first reference to Singleton.Instance, 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.Instance 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.Instance create it. It
will be transparent to calling code.

After the first reference to Singleton.Instance, 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.Instance 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
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...
4
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,...
5
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...
5
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
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. ...
8
by: Bill Cohagan | last post by:
I'm curious as to why C# doesn't support static indexers. Anybody know? Thanks, Bill
7
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...
2
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...
15
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.