Creating and initializing class with default values

1k Views Asked by At

While working on an web-application , I need to get a set of classes at few steps and I am thinking to separate this logic to a simple Factory so as based on the Class type We can create class instance as well init it with default values.

Current structure of Class hierarchy is

public interface DataPopulator<Source,Target>{
// some method decaration
}

Abstract class

public abstract class AbstractDataPopulator<Source,Target> implements DataPopulator<Source, Target>{
 // some common implimentation
}

And now classes which will be used as actual implementations like

  1. Type1Populator extends AbstractDataPopulator.
  2. Type2Populator extends AbstractDataPopulator.

Each of these implementation needs a set of common dependencies based on what functionality is being executed by those Populators. As of Now I am creating instance with new and than filling those dependencies with simple setter methods.

I am thinking about creating a simple factory pattern like

public interface PopulatorFactory{

  <T extends Object> T create(String className) throws Exception;
  <T extends Object> T create(Class populatorClass) throws Exception;
}

Abstract class

public abstract class DefaultPopulatorFactory impliments PopulatorFactory{

   public <T> T create(final Class populatorClass) throws Exception{
       return Class.forName(populatorClass);
  }
   // other method.
}

Implementation classes

public Type1PopulatorFactory extends DefaultPopulatorFactory {

      public <T> T create(final Class populatorClass) throws Exception{
           final T populator= super.create(populatorClass);
      }
}

I also want to initialize newly created instances with some default values specific to each implementation, but I'm not sure what's the best way to do this?

  1. Should I define another method say initDefaults?
  2. What is the best way to pass those dependencies to these poulators.
  3. Is the approach outlined by me fine or is it overly complicated?
1

There are 1 best solutions below

0
On

In cases when you are building not-so-trivial objects it is usually better to use the Builder pattern instead of a Factory.

In your case if you don't need external data sources you can simply write constructors for your classes where you can supply the default values and get rid of the contraption in your question.

If you use the Builder pattern you can simplify your framework by using a Builder object for the common data and a SomeOtherBuilder which extends from Builder and adds the custom values of the specialized implementation. You can make your classes constructors which are taking a Builder object.

public class Builder {

    // your fields go here

}

public class SomeOtherBuilder extends Builder {

    // your specialized fields go here

}

public class YourClass {


    public YourClass(Builder builder) {
        // construct it here
    }
}

You can also make your classes generic with using something like T extends Builder.