This discussion is archived
8 Replies Latest reply: Feb 25, 2011 8:11 PM by 801079 RSS

Generic object factory - Insights required

801079 Newbie
Currently Being Moderated
We apply factory design pattern at many places in a project. So, I've written a GenericObjectFactory class which uses a table that has [key, value] columns in it.

'Key' column would be 'unique' and 'value' would be a qualified class name.

GenericObjectFactory takes in a 'key' and gives respective instance of the qualified class name said in the DB using Java reflection.

Right now the 'instance' would be created by using the default constructor (no parameter).

I'm under the opinion to enhance 'GenericObjectFactory' to support 'parameter constructor' on need basis.

Is 'GenericObjectFactory' an overkill of a simple Factory design pattern? Should I bother about enhancing 'GenericObjectFactory' or check for work arounds if a parameter constructor is needed?

Any insights would be greatly appreciated.

Thanks in advance.
  • 1. Re: Generic object factory - Insights required
    796440 Guru
    Currently Being Moderated
    DynamicBasics wrote:
    We apply factory design pattern at many places in a project. So, I've written a GenericObjectFactory class which uses a table that has [key, value] columns in it.

    'Key' column would be 'unique' and 'value' would be a qualified class name.

    GenericObjectFactory takes in a 'key' and gives respective instance of the qualified class name said in the DB using Java reflection.

    Right now the 'instance' would be created by using the default constructor (no parameter).
    Don't describe your code in prose. Provide an [url http://sscce.org]SSCCE.
    Is 'GenericObjectFactory' an overkill of a simple Factory design pattern?
    If you're creating something that general just because it seems like a good idea, it's almost certainly overkill. If you have a specific requirement that this approach meets better than other candidates, then it's not overkill.
  • 2. Re: Generic object factory - Insights required
    801079 Newbie
    Currently Being Moderated
    jverd wrote:
    Don't describe your code in prose. Provide an [url http://sscce.org]SSCCE.
    Following should be helpful.


    /**
     * Contract for the implementations of the generic object facotry.
     * 
     * @author xxx
     * 
     */
    public interface IGenericObjectFactory {
         /**
          * Method should take key as input, and retun an object mapped to the key.
          * Please note that this contract does not enforce any method in which the
          * key-value mappings are to be stored.
          * 
          * @param key
          *            key to be used to product objects.
          * @return {@link Object} type instance.
          * @throws GenericFactoryException
          */
         Object getInstance(final String key) throws GenericFactoryException;
    }
         @Override
         public Object getInstance(final String key) throws GenericFactoryException {
              GenericObjectFactoryDAO genericObjectFactoryDAO = null;
              String className = null;
              Class c = null;
              Object o = null;
    
              try {
                   genericObjectFactoryDAO = new GenericObjectFactoryDAO();
                   className = genericObjectFactoryDAO.getProperty(key);
                   c = Class.forName(className);
                   if (c != null) {
                        o = c.newInstance();
                   }
              } catch (final ClassNotFoundException e) {
                   e.printStackTrace();
                   throw new GenericFactoryException(e);
              } catch (final InstantiationException e) {
                   e.printStackTrace();
                   throw new GenericFactoryException(e);
              } catch (final IllegalAccessException e) {
                   e.printStackTrace();
                   throw new GenericFactoryException(e);
              }
    
              return o;
         }
    Is 'GenericObjectFactory' an overkill of a simple Factory design pattern?
    If you're creating something that general just because it seems like a good idea, it's almost certainly overkill. If you have a specific requirement that this approach meets better than other candidates, then it's not overkill.
    I'm trying out interface oriented programming at home, and found out that am ending up using factory pattern quite often. With this utility at hand, an entry in database, and a simple method call is enough instead of building 'factory' whereever required.

    Edited by: DynamicBasics on Feb 16, 2011 6:51 AM
    added method implementation also.

    Edited by: DynamicBasics on Feb 16, 2011 6:52 AM
  • 3. Re: Generic object factory - Insights required
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    DynamicBasics wrote:
    Is 'GenericObjectFactory' an overkill of a simple Factory design pattern?
    Yes.

    Moreover if you are using factories so much that this seems like a good idea then you are probably misusing the factory idiom.
  • 4. Re: Generic object factory - Insights required
    801079 Newbie
    Currently Being Moderated
    Thank you jverd, jschell.

    Let me tell you where I use the factory pattern. I have the following layers in code.

    1) BusinessObjects (BO)
    2) Caching Layer
    3) DAO Layer

    Instances of above classes would be derived from the factory so that new fucntionality can be plugged in wherever the current API falls short.

    The 'application under development' will have many sub modules, and the above structure would repeat for each module. So, the factory usage would also increase.

    So, is the basic design I've followed flawed/ needs changes?
  • 5. Re: Generic object factory - Insights required
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    DynamicBasics wrote:
    Thank you jverd, jschell.

    Let me tell you where I use the factory pattern. I have the following layers in code.

    1) BusinessObjects (BO)
    2) Caching Layer
    3) DAO Layer

    Instances of above classes would be derived from the factory so that new fucntionality can be plugged in wherever the current API falls short.
    AGAIN, if you are using factories this much then you are probably misusing the idiom.

    You don't use a factory because it might be a good idea. You use a factory because you have a need that leads to that pattern.

    >
    The 'application under development' will have many sub modules, and the above structure would repeat for each module. So, the factory usage would also increase.
    The first has nothing to do with whether factories should be used. And other than that your "structure" exists because you have or think you will have a lot of factories. And AGAIN, that probably isn't a good idea to have a lot of factories. You can't justify using factories because you came up with a clever idiom for creating factories. Rather your application has a need for factories and and some point that leads to need for something like you suggest (but I doubt it.)
  • 6. Re: Generic object factory - Insights required
    801079 Newbie
    Currently Being Moderated
    jschell wrote:
    AGAIN, if you are using factories this much then you are probably misusing the idiom.

    AGAIN, that probably isn't a good idea to have a lot of factories. You can't justify using factories because you came up with a clever idiom for creating factories.
    Thankyou jschell. The idea behind this design is to let the API users change the functionality just in case. Is there any other suggested approach? Hint me to dig in that direction.
  • 7. Re: Generic object factory - Insights required
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    DynamicBasics wrote:
    jschell wrote:
    AGAIN, if you are using factories this much then you are probably misusing the idiom.

    AGAIN, that probably isn't a good idea to have a lot of factories. You can't justify using factories because you came up with a clever idiom for creating factories.
    Thankyou jschell. The idea behind this design is to let the API users change the functionality just in case. Is there any other suggested approach? Hint me to dig in that direction.
    J2EE servers use all sorts of plugins but that doesn't lead to a generic factory.

    A factory and abstract factory is sufficient. It takes no time to implement the defaults of those. What takes time is implementing the actual plugins and implementing the framework that uses the plugins.

    And not everything can be nor should be replaceable.
  • 8. Re: Generic object factory - Insights required
    801079 Newbie
    Currently Being Moderated
    Thankyou jschell.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points