dimanche 22 mai 2016

Config class to set object state

I have many classes that need to produce objects that have a lot of state. This could be done using setters and then instantiating the objects, using the appropriate setter methods. A builder class could also be used in this case.

However, I want to be able to load from a configuration file so that I only have to instantiate the object with a no-args constructor. I have a separate utils package that has a config class in it that I use for other packages which will contain many fields using the Config class (the config class is set to read from a default properties file):

public class Example {
   private static String aField;

   static {
       aField = Config.getProperty("aField");
   }

   public Example(){
       //
   }

   /* public methods
       //
   */
}

Thus, all my other packages have a dependency on this utils package. Is this good design or should I be doing this another way?

Aucun commentaire:

Enregistrer un commentaire