Singleton help please.

  • Thread starter Thread starter VidalSasoon
  • Start date Start date
V

VidalSasoon

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?

V.
 
Hi,
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.

Singleton == always the same object, always the same instance. If you want
that, use two static methods:

====================

public class SingleClass {

private SingleClass instance;

public static SingleClass InitObject( <parameters> ) {
if ( this.instance == null )
this.instance = new SingleClass( .... );

return this.instance
}

public static SingleClass GetInstance() {
if ( this.instance != null )
return this.instance;
else
return null;
}

private SingleClass( ... ) {

// Init
}
}

======================

Regards,

Frank Eller
www.frankeller.de
 
VidalSasoon said:
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.
 
Frank Eller said:
Singleton == always the same object, always the same instance. If you want
that, use two static methods:

public static SingleClass GetInstance() {
if ( this.instance != null )
return this.instance;
else
return null;
}

This won't compile, as "this" doesn't exist in a static method. Even if
it did, the if clause is somewhat redundant, is it not? The above is
exactly equivalent to

public static SingleClass GetInstance()
{
return this.instance;
}

(which still doesn't compile, of course).
 
VidalSasoon said:
[...]
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.
[...]

Hi,

take a look at this singleton implementation:

public sealed class Singleton

{

private static readonly Singleton m_Instance = new Singleton( );



private Singleton( ) { }



static Singleton( ) { }



public static Singleton Instance

{

get { return m_Instance; }

}

}



thread-safe and good performance.



<http://weblogs.netug.de/hgzigann/articles/329.aspx>
 
Heck, you're right. I shouldn't write down stuff right from my mind ...
*Sigh* ...

Regards,

Frank
 
Alternatively...

using System;

namespace TestStatic1
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
///

static private int classID= 0;
static private bool isInit= false;
static public bool InitClassID(int i)
{
lock(typeof(Class1))
{
if (isInit || i<0) {return false;}
else
{
isInit= true;
classID= i;
return true;
}
}
}
static private int GetClassID()
{
lock(typeof(Class1))
{
return classID++;
}
}

private int id;
public Class1()
{
if (!Class1.isInit)
{
throw new IndexOutOfRangeException("Class Not Init.");
}
else
{
this.id= GetClassID();
}
}
public int ID
{
get{return id;}
}


[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
try
{
Class1 c1= new Class1();
System.Console.WriteLine(c1.ID); // 1
}
catch (Exception e)
{
System.Console.WriteLine(e);
}
Class1.InitClassID(100);
Class1 c2= new Class1();
System.Console.WriteLine(c2.ID); // 100
Class1.InitClassID(100);
Class1 c3= new Class1();
System.Console.WriteLine(c3.ID); // 101
System.Console.ReadLine();

}
}
}


Regards,
Jeff
 
Hans-Gerd Zigann said:
VidalSasoon said:
[...]
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.
[...]

take a look at this singleton implementation:

public sealed class Singleton
{
private static readonly Singleton m_Instance = new Singleton( );

Yes, that's a fine Singleton implementation (although it sacrifices
some performance in order to get true laziness). However, it doesn't
address the OP's issue of initializing the singleton with parameters.
 
Jon Skeet said:
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.

I'm making a 3d app so I call this singleton about 100 times a second.
I tried it without locks and it seems it would sometimes get a
NullReferenceException. I think I know what I did wrong though. It's
gonna work.

thanks for all the info :)
V.
 
VidalSasoon said:
I'm making a 3d app so I call this singleton about 100 times a second.
I tried it without locks and it seems it would sometimes get a
NullReferenceException. I think I know what I did wrong though. It's
gonna work.

100 times a second is no problem at all if you need to go with a
solution which uses locks. If you were going to use the property
hundreds of *thousands* of times a second, that might be different.
Don't forget that you can always call the property once and use the
same reference several times.
 
Back
Top