Thursday, 21 April 2016

Scenario : How to instantiate class via factory class ?


Scenario

Sometimes it may be necessary to instantiate a class via another class (usually called a Factory class). In this scenario, Spring should not create the class on its own but simply delegate the instantiation to the Factory class.

Using factory-method attribute of bean tag
Essentially, there is a static method defined in the Factory class that creates the instance of the required bean.
Typically, factory-method is used in application integration where objects are constructed in a more complex way using a Factory class.


Real time Example
  • Create an ATM class and Printer class.
    The ATM class depends on the Printer class to perform printing related responsibilities like printing the balance information for an account.
  • Assume that creating the instance of Printer involves complex operationsand it would be better to use a PrinterFactory class to instantiate the Printer class.
  • Create a PrinterFactory class with static createPrinter() method and returns the Printer type.
  • Create the spring-config.xml and use the factory-method attribute to instantiate Printer class using PrinterFactory.createPrinter()method.
  • Load Spring context and get a reference to ATM class. Then, Print the balance information for an account using the ATM class and verify that the Printer class has been successfully injected into ATM class using factory-method. 

Java classes
public class ATM {     
   private Printer printer;     
    // getter-setter     
    public void printBalanceInformation(String accountNumber) {
        getPrinter().printBalanceInformation(accountNumber);     
   }
 }

public class Printer {
     public void printBalanceInformation(String accountNumber) {
        System.out.println("The printer has printed the balance information for the account number " + accountNumber);
      } 
}

public class PrinterFactory {
     public static Printer createPrinter() {
        return new Printer();    
     }
}


Spring configuration (spring-config.xml)
  • Declare bean for ATM class with the property by referring to the printer bean.
  • Declare a bean with id 'printer', which class attribute points to PrinterFactory class.
  • Declare the factory-method attribute with value 'createPrinter'.

    Note
     : We are using the PrinterFactory.createPrinter() method to instantiate the Printer class

<?xml version="1.0" encoding="UTF-8"?>
<beans ....>
    <bean id="atm" class="com.shaan.ATM">
       <property name="printer" ref="printer" />
    </bean>

    <bean id="printer" class="com.shaan.PrinterFactory 
          factory-method="createPrinter">
    </bean>
</beans>



Test this now !

The main program
ApplicationContext context = 
        new ClassPathXmlApplicationContext("spring-config.xml");
ATM atm = (ATM) context.getBean("atm");
String accountNumber = "AC5645786";
atm.printBalanceInformation(accountNumber);

No comments:

Post a Comment

Note: only a member of this blog may post a comment.