Announcement Announcement Module
Collapse
No announcement yet.
Factory design pattern ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Factory design pattern ?

    I found an example in some blog and implemented the following.
    Code:
    public class WriterFactory implements FactoryBean {
    	@Autowired
    	private TextItemWriter textWriter;
    	@Autowired
    	private XMLItemWriter xmlWriter;
    
    	@Override
    	public Object getObject() throws Exception {
    	if (/*check for text*/)
    		/* Is it better approach to create instance using new operator or get proxy object using autowire*/
    		/* return new TextItemWriter(); */
    		return textWriter;
    	if (/*check for xml*/)
    		return xmlWriter;
    	}
    
    	@Override
    	public Class getObjectType() {
    	/* The Class Type to which the object will be typecasted */
    	return WriterInterface.class;
    	}
    
    	@Override
    	public boolean isSingleton() {
    	return true;
    	}
    }
    Code:
    <batch:job id="dataExtractjob" incrementer="lastRunSetter" >
          <batch:step id="extract" >
    	<batch:tasklet >
    	         <batch:chunk reader="readerFactory" writer="itemwriterFactory"	commit-interval="10"  />
    	</batch:tasklet>
          </batch:step>
    </batch:job>
    
    <bean id="itemwriterFactory" class="com.mycompany.factory.WriterFactory"	scope="step"/>

    Please have a look at the example code.
    1. Is this implementation of factory design pattern or not?
    is it prefered approach?

    2. I want spring container to manage the object so i am using
    @Autowired
    private TextItemWriter textWriter;
    is this prefered approach. if so when is the proxy object created
    (when this factory object is initialized or at runtime when we try to access/set some properties for the object reference)
    Last edited by kamath_sv; Jun 6th, 2011, 11:56 PM. Reason: restructuring removing unnecessary spaces

  • #2
    1)
    Looks like a valid FactoryBean to me. The factory itself is a Singleton and Spring does detect it implemented FactoryBean interface and treats it accordingly for the <bean ..../> definition.

    Your factory should not have either of those 2 private fields. Those should be local variables in getObject(). The pattern does not really instate a new instance if you keep these 2 private fields, it acts more like an instance (Service) Locator pattern IMHO. I'm pretty sure a Factory MUST return a new concrete instance (that never existed before getObject() was called) this is what makes it a Factory.

    http://static.springsource.org/sprin...anFactory.html maybe used so that would be:
    AutowireCapableBeanFactory acbf = ApplicationContext.getAutowireCapableBeanFactory() ;
    WriterInterface wi = acbf.createBean(TextItemWriter.class);
    return wi;
    // code above is a guess, not tested


    It is my understanding that if the TextItemWriter (or XmlItemWriter) implementation itself uses any @Autowired then you need to get Spring to instate it. If you are not sure and want maximum future compatibility and the rate at which the factory creates a new instance is not really a performance concern, then might as well always use Spring to instante the object.

    Or course @Autowired is not the only reason why you might wish to make Spring instate the getObject() instance, you might want to use other Spring features like AOP or whatever they invent next. It will automatically be available without you needing to modify your Factory code. You can also implement both methods and make it a boolean config option, make it spring aware by default.

    2)
    Also your usage will be:
    @Autowired
    private WriterInterface textWriter;

    If you really want the usage you say from application code:
    @Autowired
    private TextItemWriter textWriter;

    Then I think you would need to instate 2 <bean .../> one for each type that would be returned by getObjectType(); You would have 2 factorys returning appropiate getObjectType().

    But then you have to wonder if you need your own FactoryBean since it is not clear that you are doing anything special in getObject() when you instate the concrete type. The code as-is Spring itself could instate your types on demand. (This presume you really want a Factory and not a Locator)
    Last edited by dlmiles; Jun 7th, 2011, 01:10 PM.

    Comment


    • #3
      Thanks dlmiles for so much information.
      the one thing which is still not clear to me is
      "Is using FactoryBean same as implementing factory Design pattern using factory-method".

      Comment


      • #4
        http://static.springsource.org/sprin...-factory-class (around 3.3.2.3)

        I believe factory-method simpy allows you to change the name from the default of getObject() method to something else not sure how getObjectType() works. Maybe you can return the type directly and the getObjectType() auto-configures itself from the method return value for the method signature found ?

        There is even a brief example of a Locator pattern in that documentation section, base around reusing the private field to store the located value that each invoker gets returned.

        Comment

        Working...
        X