Passing parameters to singleton class / static constructor

3.7k views Asked by At

I have a Listener class to listen on a particular HTTP port. I made Listener singleton (since there will always be a single listener object listening on a particular port). For this I made default Listener() contructor private. Made Listener instance (inside Listener class) static and have static Listener constructor initialize singleton Listener object. Also have static Listener getListener() which returns singleton Listener instance.

I am receiving a SOAP on this port which my listener callback method is processing.

Now I want date time of last notification pushed to my UI.

So what I thought is somehow while initializing Listener I will pass a delegate, a reference to which will be stored inside Listener and then every time I receive a notification, inside listener callback I will call that delegate and pass it current time (new DateTime()), which will do necessary task of printing message with time. I also thought of making delegate parameter optional so that if delegate is null it will not be called at all on notification. However since I made constructor private, I am not able to figure it out how I can pass delegate to it.

To summarize and generalize the whole question: How to pass a parameter (to be stored as a instance member) to singleton class?

Should I pass delegate to getListener()? and inside it check every-time if delegate is null and if not, set it? But then if I am accessing this singleton object too many times by calling Listener.getListener(), everytime it will unnecessarily check the delegate is null. In fact thats why I moved initializing Listener to static constructor just to avoid checking if Listener is initialized everytime getListener() is called.

What should I do to pass parameter to singleton class? Or this checks in getListener() inevitable?

Note: I don't know but this question may boil down to how can we pass parameters to static constructor since we need to initialize things first time the class is used and we have to initialize it with delegate/other programmatic construct (thus we cannot load initialization parameters from some config file inside static constructor).

1

There are 1 answers

1
Roland Bär On BEST ANSWER

Just add a normal event to your singleton class:

public class Listener
{
  //declare a delegate for the event
  public delegate void InformationDelegate(DateTime timestamp);

  //declare the event using the delegate
  public event InformationDelegate Information;

  public void SomeFunction()
  {
    // do something...
    if(Information != null)
    {
      Information(DateTime.Now);
    }
  }

  // singleton handling etc... 
}

Then every object that wants to get informed registers itself on the singleton instance:

class SomeUIClass
{
  void AFunction()
  {
    Listener.Instance.Information += InformationHandling;
  }

  public void InformationHandling(DateTime timestamp)
  {
    // do something with the information
  }
}

Thanks to @Kami for clarifying the topic!