473,616 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1416
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.co m
"Amadelle" <am******@yahoo .com> wrote in message
news:%2******** *******@TK2MSFT NGP10.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 SystemInformati on 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******** *******@TK2MSFT NGP10.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******** *******@TK2MSFT NGP10.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
674
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 generic design patterns that can be used and shared amongst many sub-schemas. For example, the grouping of entities. I may have the following tables: employee, product and client. These tables have no direct relationship with each other. But...
9
2919
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 a device. The definition of the table is as follows: CREATE TABLE devicedata ( device_id int NOT NULL REFERENCES devices(id), -- id in the device
2
2440
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 experts. I've been asked to create a Daily Log sheet to be distributed to some of our clerks. For each day, the clerk is to log tasks worked on for the day, (i.e worked on the johnson account).
6
2111
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 responsible for instantiating the orders class? Would it be the ui layer or the master class in the business layer? thanks,
17
2690
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 know nothing about ASP. He can build the design of the pages in HTML with tables, labels, textboxes etc. But then I would need to change them to ASP.net objects and write the code to make the page work (normally I do this as I go - can't do this...
17
4842
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 Page Is Valid XHTML 1.0 Transitional!" but it still wouldn't look the same. It is on http://www.dvdnowkiosks.com/new/theproduct.php scroll down and recognize the black bottom bar when you go ewith firefox(2.0) which isn't there with IE7. Why does...
6
2132
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 times when I program, I will create objects for a specific purpose for a program and if I need to add to it I just add the code.
0
2069
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 end of this message, but I will start with an overview of the problem. I've made a content management solution for my work with a decently structured relational database system. The CMS stores articles. The CMS also stores related items --...
19
3155
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 certification, possibly that involves C++, but, if not, C++ and UML. All I could find was Java + UML design certifications (one such is detailed on http://www.objectsbydesign.com/tools/certification.html). Although UML is expected to be language independent,...
8
2217
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, port number, or both. How should we design the classes to represent these filters? My answer was: class FilterRule {
0
8199
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
8642
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
8294
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
8448
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
7118
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...
1
6097
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
5550
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
4140
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2576
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

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.