Tuesday, December 11, 2012

Design Patterns : Factory Pattern 2


Finally we come to the main class of our little POC
We now focus our attention to the factory class ChildFactory
ChildFactory which essentially instantiates a child whether
it is a Boy or a Girl


package com.company.self.study.core.java;

/**
*
* Factory Pattern Example
*
* @author administrator
*
*/
public class ChildFactory {

   private static ChildFactory instance = new ChildFactory();

/**
*
*
*
*
* @return
*/
   public static ChildFactory getInstance() {
      return instance;
   }

/**
*
*
*
* @param age
* @param isMale
* @return
*/
   public Child getChild(int age, boolean isMale) {
      Child child = null;
      if(isMale) {
         child = new Boy(age);
      }
      else {
      child = new Girl(age);
      }
      return child;
   }
}


As we can see this class is a Singleton and
has a static object creator. Apart from that the
only other method getChild(age, isMale) the class has
actually does the bulk of the work. It returns the Boy class
if the isMale argument is true. Else it would return the Girl class


Finally we have the ChildFactoryTester class which
actually tests the application. The below listing includes it.



package com.
company.self.study.core.java;


/**
*
* Factory Pattern Example
*
*
* @author administrator
*
*/
@ClassTemplate(date="12/05/2012")
public class ChildFactoryTester {

/**
* @param args
*/
   public static void main(String[] args) {
// TODO Auto-generated method stub
      System.out.println(" ChildFactoryTester start ");
      Child firstIssue = ChildFactory.getInstance().getChild(5, true);
      Child secondIssue = ChildFactory.getInstance().getChild(3, false);
      System.out.println(" firstIssue " + firstIssue + " secondIssue " + secondIssue);
      System.out.println(" ChildFactoryTester end ");
}

}


The below listing is for the Class Annotation which
encapsulates the date of the program and the name of the programmer.




package com.
company.self.study.core.java;

public @interface ClassTemplate {
   String author() default "[Sanjay Ghosh]";
   String date() default "[1/1/2001]";
}



Now we are ready to run the Tester through a command script and
the pipe the output to an Output File. Below are the listings for the script
and the final output.


Command Script


"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\company\self\study\core\java\ClassTemplate.java

"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\
company\self\study\core\java\Father.java

"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\
company\self\study\core\java\Mother.java

"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\
company\self\study\core\java\Child.java

"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\
company\self\study\core\java\Boy.java

"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\
company\self\study\core\java\Girl.java

"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\
company\self\study\core\java\ChildFactory.java

"C:\Program Files\Java\jdk1.6.0_26\bin"\javac -classpath bin -d bin src\com\
company\self\study\core\java\ChildFactoryTester.java

java -classpath bin com.
company.self.study.core.java.ChildFactoryTester > Output_ChildFactoryTester.txt



Output File


ChildFactoryTester start
firstIssue Boy of 5 yrs secondIssue Girl of 3 yrs
ChildFactoryTester end


No comments: