4 Replies Latest reply: Mar 1, 2013 2:37 AM by r035198x RSS

    Supporting runtime parameters through Setter method vs Constructor

    800839
      Hi,

      I want to support run time operation , which one should be more adequate Setter method or Constructor?
      Any examples are highly appreciated on this.

      Thanks.
        • 1. Re: Supporting runtime parameters through Setter method vs Constructor
          gimbal2
          Define what you think "runtime operation" is.
          • 2. Re: Supporting runtime parameters through Setter method vs Constructor
            800839
            Thanks.

            I am using an abstract factory to return instances of concrete subclasses. I would like to instantiate the subclasses at runtime given a String of the concrete class name. I also need to pass a parameter to the constructors.

            This is what I have done:-

            abstract class Parent {

            private static HashMap<String, Child> instances = new HashMap<String,Child>()

            private Object constructorParameter;

            public static Child factory(String childName, Object constructorParam){

            if(instances.keyExists(childName)){
            return instances.get(childName);
            }

            //Some code here to instantiate the Child using constructorParam,
            //then save Child into the HashMap, and then return the Child.
            //Currently, I am doing:
            Child instance = (Child) Class.forName(childClass).getConstructor().newInstance(new Object[] {constructorParam});
            instances.put(childName, instance);
            return instance;
            }

            //Constructor is protected so unrelated classes can't instantiate
            protected Parent(Object param){
            constructorParameter = param;
            }

            }//end Parent

            class Child extends Parent {
            protected Child(Object constructorParameter){
            super(constructorParameter);
            }
            }
            My attempt above is throwing the following exception: java.lang.NoSuchMethodException: Child.<init>(), followed by the stack trace.

            Any help is appreciated.

            Thanks
            • 3. Re: Supporting runtime parameters through Setter method vs Constructor
              939520
              Well, I can't comment on what's causing your exception, but I question the following code:

              if(instances.keyExists(childName)){
              return instances.get(childName);
              }

              Your returning an object if it already exists. But shouldn't you configure it with the proper initialization parameters first? Doesnt your collection of existing objects have the old parameters in them that you don't want to reuse? Mind you, I haven't really studied your code so I may be off-base on this.

              As far as setter vs constructor, my suggestion is never return an object that is in an invalid state. Therefore the constructor should have at least the minimal arguments necessary that makes it valid. You can still have setters for both required and optional values.
              • 4. Re: Supporting runtime parameters through Setter method vs Constructor
                r035198x
                You shouldn't need to use reflection to implement an abstract factory pattern. See here for an example http://en.wikipedia.org/wiki/Abstract_factory_pattern

                What's instances.keyExists ? HashMap has containsKey. Did you post the exact code you are using? Please post it exactly and use code tags while posting it.