title: Singleton class with parameter
body: <p>I'm developing computer vision application and I will need Classifier class. This class will be immutable per run of application and it loads the trained data from disk on initialization. I want to ensure that whole program will have access to same trained data and I want to block reloading the from disk once they are loaded.</p>
  
  <p>What I was considering was to use either static class or singleton. I'm not sure how to load data to static class, because the path to data file is not know at compile time - it will be program argument. So I was thinking of Singleton pattern, but there I don't know how to initialize it dynamically.</p>
  
  <p>My idea was to use following:</p>
  
  <pre><code>class Singleton {
      private static Singleton instance;
      private Singleton() { ... }
      private static SomeDataObject data;
  
      public static Singleton getInstance() {
          if(instance == null)
              instance = new Singleton();
  
              return instance;
      }
  
      public static init(string dataPath){
          if(data == null)
               loadDataFromFile(dataPath)
      }
  }
  </code></pre>
  
  <p>This would <strong>not work</strong>, because I have no control which method will be called first.</p>
  
  <p>I know the proper way would be to create the instance with data at the begining and pass it to all classes and methods which need it, but that's not really general solution. I can keep track of all calls to Classifier in my own code, but if I would make the code as API, this would be a problem.</p>
  
  <p>In short how to initialize singleton at runtime?</p>