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

OO design question

Hi All and thanks in advance,

I wanted to know when is a good idea to use a static class (with static
constructor) and when to use instance classes? I have read couple of
articles on line and the general example is when you want to write a log
file use static class ...but they don't say why?

My specific question is that I have a validation class with some data
validation methods which will be called through out the program and I am
debating whether this validation class should I set it up as a static class
or an instance class?
What about my data access class with methods for accessing database, opening
up connections and running queries? Should I set that up as an instance
class or static class?

Thanks I appreciate your help in this matter,

Amadelle
Nov 16 '05 #1
3 1396
Amadelle,

Quite simply, it is all about scope. Anything static is in the context
of the current app domain. At that point, what you need to ask is, "within
this context, is what I am writing instance specific or type specific?".

Take the Math class, for example. It has static functions for computing
the sine, cosine, tangent, etc, etc on angles. Now if there was a concrete
representation of an angle in the system, then I would have Sin, Cosine,
etc, etc, be properties on it, but since angles are represented easily
enough with numbers (eliminating the need for a class), it makes sense to
just have a function that doesn't need an instantiation of an object to make
the call on for this information.

However, take a class that represents people. You have specific
information that is unique to each individual person that the class can
represent. Accounting for all of these differences would require multiple
instances of the class, each configured to represent people in a different
way. Having this be static would make it difficult to deal with a specific
person in your code.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Amadelle" <am******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Hi All and thanks in advance,

I wanted to know when is a good idea to use a static class (with static
constructor) and when to use instance classes? I have read couple of
articles on line and the general example is when you want to write a log
file use static class ...but they don't say why?

My specific question is that I have a validation class with some data
validation methods which will be called through out the program and I am
debating whether this validation class should I set it up as a static class or an instance class?
What about my data access class with methods for accessing database, opening up connections and running queries? Should I set that up as an instance
class or static class?

Thanks I appreciate your help in this matter,

Amadelle

Nov 16 '05 #2
Hi, Amadelle

Standard recommendation - check some books on OO programming and design,
like Booch, Grady and many others. However, it's a lot of reading, which is
not very thrilling :-)

Main difference is - static objects usually do not change / evolve during
application lifetime. Instantiated objects are dynamic by nature. Depending
on object domain they are created and destroyed as result of changes in
environment - user actions, external events etc.
Static is really static and instantiated is dynamic.

So, rule of thumb is
- if object behavior and data does not depend on application environment and
should provide always same results - object is most likely static. If it
should be visible and accessible during whole application life time - it has
to be static. Like page in book. Whenever you look at it you see same
letters. Global values are good example. And static classes in .Net like
Environment or SystemInformation are good examples.
- when your objects are changing and multiply / die during application life
time, when objects behave in different way if another object changes state,
they are most likely instances. Code word here is stateful - at any given
moment object has state, which might be changed and change its behavior.

Usually loggers are static because they must be visible and accessible
whatever other objects exist in application. You create queues, threads,
forms, but you must be able to log events using same log object from any of
these and at arbitrary moments. Because you write book and do not wipe out
logged information.

With database issue is usually more complex - databases reflect dynamics of
data. Today customer lives here, tomorrow there, so you never know which
results you will get with same query. Another issue is scalability and
resource usage - you can't hold locks on data forever, you can't open and
hold connections for every connected user for 9-10 hours and so on. So,
usually these things are very dynamic and implemented as instantiated
classes. At the same time, instantiated classes might implement methods,
which are always doing same jobs, like for example running query and
returning result set. Such method is static by nature and could be
implemented as static. However if same method has to provide different
functionality for different instances (say, for Peter it has to return his
date of birth and for Maria her date of death) it might be better
implemented as instance method. Like notepad - you add pages, remove them,
scribble some, then overwrite etc.

This is very simplistic intro static vs. instance issues. In most cases best
solution is to model object domain and analyze where you have static parts
and behaviors and where you have dynamics. That's why I mention OO design
theory - it takes some experience in addition to theoretical ground to be
able to make sound decisions in this area.

HTH
Alex

"Amadelle" <am******@yahoo.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Hi All and thanks in advance,

I wanted to know when is a good idea to use a static class (with static
constructor) and when to use instance classes? I have read couple of
articles on line and the general example is when you want to write a log
file use static class ...but they don't say why?

My specific question is that I have a validation class with some data
validation methods which will be called through out the program and I am
debating whether this validation class should I set it up as a static class or an instance class?
What about my data access class with methods for accessing database, opening up connections and running queries? Should I set that up as an instance
class or static class?

Thanks I appreciate your help in this matter,

Amadelle

Nov 16 '05 #3
"Amadelle" <am******@yahoo.com> wrote in
news:%2***************@TK2MSFTNGP10.phx.gbl...
Hi All and thanks in advance,

I wanted to know when is a good idea to use a static class (with static
constructor) and when to use instance classes? I have read couple of
articles on line and the general example is when you want to write a log
file use static class ...but they don't say why?
I'm not 100% sure, but I think when you talk about "static classes", you
really mean singletons?

(Actually, I only know the term "static inner/nested class" from java, where
inner classes usually can access the outer classes members, but static inner
classes can't. From that point of view C# (to my knowledge) like C++ inner
classes are always static.)

Now, it's quite common to implement a log file class as a singleton because:
a) it's used in many unrelated classes
b) all places should write to the same logfile
So you could either pass a log-file-pointer all around through your class
hirarchy (which is ugly), or you could simply implement it as a singleton.
My specific question is that I have a validation class with some data
validation methods which will be called through out the program and I am
debating whether this validation class should I set it up as a static class or an instance class?
It depends on your application:
- does "called through out the program" really mean called from completely
unrelated classes?
- do those different classes actually need to use the same instance of your
validation class, or couldn't each of those simply create its own instance?
- do you have any "client-dependent" information in your validation class,
that could be lost if two clients use the same object without knowing?
What about my data access class with methods for accessing database, opening up connections and running queries? Should I set that up as an instance
class or static class?


This is similar to the above problem, however a database access class does
"cost" more: you wouldn't want more than one database connection at a time;
So in this case a singleton would probably make more sense.

Niki
Nov 16 '05 #4

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

Similar topics

5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
9
by: sk | last post by:
I have an applicaton in which I collect data for different parameters for a set of devices. The data are entered into a single table, each set of name, value pairs time-stamped and associated with...
2
by: Test User | last post by:
Hi all, (please excuse the crosspost as I'm trying to reach as many people as possible) I am somewhat familiar with Access 2000, but my latest project has me stumped. So, I defer to you...
6
by: rodchar | last post by:
Hey all, I'm trying to understand Master/Detail concepts in VB.NET. If I do a data adapter fill for both customer and orders from Northwind where should that dataset live? What client is...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
17
by: roN | last post by:
Hi, I'm creating a Website with divs and i do have some troubles, to make it looking the same way in Firefox and IE (tested with IE7). I checked it with the e3c validator and it says: " This...
6
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
19
by: neelsmail | last post by:
Hi, I have been working on C++ for some time now, and I think I have a flair for design (which just might be only my imagination over- stretched.. :) ). So, I tried to find a design...
8
by: indrawati.yahya | last post by:
In a recent job interview, the interviewer asked me how I'd design classes for the following problem: let's consider a hypothetical firewall, which filters network packets by either IP address,...
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...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: 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
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
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...

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.