title: Pass objects in a standardized way to interfaces/abstract classes
body: <p><strong>Background:</strong> I often ask myself the following question and I never found an answer without flaws and I think this is a common problem in modularized software development. So I am not quite sure if programmers.stackexchange.com is more suitable - but this question is totally focused on Java and I want a Java solution:</p>
  
  <p><strong>Example:</strong> There is a text editor that loads several plugins who are responsible for their file type interaction in the text editor (Java plugin handles Java code/files, C/C++ plugin handles C/C++ code/files etc.) - each plugin needs a configuration and access to the file manager. These plugins have to access an application API to do some useful stuff without reinventing the wheel. Keep in mind, that for example the file manager of the application also needs data/objects itself (For example a configuration with line separator type, type of read mode etc.).</p>
  
  <p><strong>Question:</strong> How can I pass the configuration/file manager to an pugin (Based on an interface or abstract class) in a modularized application when the file manager also need a configuration itself?</p>
  
  <p><strong>Problems/Things to keep in mind:</strong></p>
  
  <ul>
  <li>We need to pass data to the file manager (Configuration etc.)</li>
  <li>Java doesn't support custom constructors in interfaces and abstract classes</li>
  <li>Annotation'ed objects are hard to test and often the root of all evil</li>
  <li>Try to create a proper/clear API and make it hard as possible to abuse/use the API in a wring way</li>
  </ul>
  
  <p><strong>Solution 1:</strong> Use dependency injection via annotation in the plugins, so we can get the configuration and file manager and use them in our plugins. This is the smoothest solution but not that easy to test.</p>
  
  <pre><code>public class JavaPlugin implements Plugin
  {
      @Autowired
      Configuration configuration;
  
      @Autowired
      FileManager filemanager;
  
      public String processText(String rawtext)
      {
          // Do stuff and access the configuration/file manager
      }
  }
  </code></pre>
  
  <p><strong>Solution 2:</strong> Make the file manager a singleton ... but wait, how do we pass the configuration to the singleton? Make another static method that takes these parameters? That's not really a singleton anymore and the hell of a proper API.</p>
  
  <pre><code>public class JavaPlugin implements Plugin
  {
      public String processText(String rawtext)
      {
          FileManager filemanager = FileManager.getInstance();
          Configuration configuiration = filemanager.getConfiguration();
          // Do stuff and use the configuration/file manager
      }
  }
  
  public class FileManager
  {
      private static FileManager filemanager;
  
      private Configuration configuration;
  
      private FileManager(Configuration configuration)
      {
          this.configuration = configuration;
      }
  
      public static FileManager getIntialInstance(Configuration configuration)
      {
          if(filemanager == null)
          {
              filemanager = new FileManager(configuration);
          }
          return filemanager;
      }
  
      public static FileManager getInstance()
      {
          return filemanager;
      }
  
      public Configuration getConfiguration()
      {
          return configuration;
      }
  }
  </code></pre>
  
  <p><strong>Solution 3:</strong> Just tell all plugin developers to implement a constructor that takes a configuration and a file manager as parameters (Because Java doesn't support custom constructors in an interface/abstract class). We create the plugin objects with reflection and pass the configuration and file manager to the constructor. But what happens if we need a third object? </p>
  
  <pre><code>public class JavaPlugin implements Plugin
  {
      public(Configuration configuration, FileManager filemanager)
      {
          // Save them
      }
  
      public String processText(String rawtext)
      {
          FileManager filemanager = FileManager.getInstance();
          Configuration configuiration = filemanager.getConfiguration();
          // Do stuff and use the configuration/file manager
      }
  }
  
  public class PhpPlugin implements Plugin
  {
      public PhpPlugin()
      {
          // Oh dear you just broke your plugin....
      }
  
      public String processText(String rawtext)
      {
          // Do stuff and use the configuration/file manager
      }
  }
  </code></pre>
  
  <p><strong>Final thoughts:</strong> I am not 100% satisfied with one of these 3 solutions - everyone has flaws. I prefer a puristic that forces a developer to follow the API (For example he should not be able to use a singleton anywhere - he should get the data and if he needs them he has to store them - otherwise he has to deal with it).</p>
  
  <p><strong>StackOverflow question:</strong> Are there other ways to "solve" this problem (Maybe thoughts I missed)?</p>