473,398 Members | 2,403 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,398 software developers and data experts.

Static versus Singleton versus seperate Instantiations...

while working in a probject, you realise that some string handling stuff
you're implementing has occurred before in the same project. So being a good
developer, you factorise the commonalities out and into some "StringMethods"
type class...

Would you:
- create this object and use that instance each time to access the methods
you need
- use a singleton structure, so that only one instance is ever created
- use public statics, so that you never need to create a host instance in
the first place

Would the same principle apply to global constants?

I've been using public statics since the move to c# and wondered if this was
the best approach.

Thanks for your time.

Daniel.
Nov 16 '05 #1
3 1195
Dan Bass wrote:
I've been using public statics since the move to c# and wondered if this
was the best approach. Yes, I would say using static methods is definitely the best approach.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:Oc*************@TK2MSFTNGP11.phx.gbl... while working in a probject, you realise that some string handling stuff
you're implementing has occurred before in the same project. So being a
good developer, you factorise the commonalities out and into some
"StringMethods" type class...

Would you:
- create this object and use that instance each time to access the methods
you need
- use a singleton structure, so that only one instance is ever created
- use public statics, so that you never need to create a host instance in
the first place

Would the same principle apply to global constants?

I've been using public statics since the move to c# and wondered if this
was the best approach.

Thanks for your time.

Daniel.

Nov 16 '05 #2
Dan:

Good question. Use public (or internal) static methods instead of a
Singleton. Singletons would be the choice if you had a class that actually
needed to sit around in memory and maintain the state of something (for
example, if you wanted something to act kind of like an in-process "server".

-Dave
"Dan Bass" <danielbass [at] postmaster [dot] co [dot] uk> wrote in message
news:Oc*************@TK2MSFTNGP11.phx.gbl...
while working in a probject, you realise that some string handling stuff
you're implementing has occurred before in the same project. So being a
good developer, you factorise the commonalities out and into some
"StringMethods" type class...

Would you:
- create this object and use that instance each time to access the methods
you need
- use a singleton structure, so that only one instance is ever created
- use public statics, so that you never need to create a host instance in
the first place

Would the same principle apply to global constants?

I've been using public statics since the move to c# and wondered if this
was the best approach.

Thanks for your time.

Daniel.

Nov 16 '05 #3
Dan.... Correct me if I am wrong , but I must assume that the cost of a
static
method and a singleton method is going to be similar except maybe a
pointer
to this in the stack frame for a singleton. As best I understand it, a
new "stack
frame" is generated for every call to a static method, and this "stack
frame"
contains a copy of the method arguments and stores any new objects
created
in the static method.

My understanding is to use static methods unless you want to support a
level
of indirection provided by the SingletonPattern.

Regards,
Jeff
- use a singleton structure, so that only one instance is ever created

- use public statics, so that you never need to create a host instance
in
the first place<
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

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

Similar topics

17
by: Picho | last post by:
Hi all, I popped up this question a while ago, and I thought it was worth checking again now... (maybe something has changed or something will change). I read this book about component...
8
by: Bill Cohagan | last post by:
I'm curious as to why C# doesn't support static indexers. Anybody know? Thanks, Bill
4
by: DBC User | last post by:
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...
19
by: =?Utf-8?B?Sko=?= | last post by:
I have a logging component that I will access in other assemblies. So it was brought up to me that I should pass an instance around to these components instead of just making the logging class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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,...
0
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...

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.