VidalSasoon <vi*********@bootbox.net> wrote:
I am trying to set up a singleton since it seems like the best
solution to my problem.
I have a an object that I want to initialize when the program starts
then I just want to call it like a static function.... so i want to
create instances of that object. I need to send parameters to
initialize my object properly in the singleton. that is where i am
starting to have problems. I starting to wonder if this is bad style.
is this the bast way to go?
Well, you could adapt the normal singleton pattern by having a method
to set the instance (with parameters), and a property which returns the
instance if it's been set up and throws an exception otherwise.
(The set method would throw an exception if called twice.)
For true thread-safety, you should make sure there's a memory barrier
between the set and every get, but chances are you'll be fine without
it. If you want it though, just using a simple lock for both the set
and the get would do the trick nicely, and won't cost too much unless
you're fetching the instance *loads* of times.
Another alternative is to use the normal singleton pattern (see
http://www.pobox.com/~skeet/csharp/singleton.html) but make the
initializer fetch the parameters from wherever it needs to.
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too