Thursday, July 29, 2010

Factory Method Patterns (Implementation )

1-Factory Method is base on the Creations Pattern .
2-This Pattern is used to monitoring the type of instance of the class at the run time .
3-Calender Class is based on the factory Method
4-getInstance() static method is create different Class  Object at run Time.
Example :
In this example i try to explain  the Factory pattern .
Report  class  is responsible  to create type of reportObject  on the runtime .
Type Of Report
1-PDFReport
2-HTMLReport
3-ExcelReport
4-TEXTReport
5-XMLReport

//Report Class
-------------------------------------------------------------------------------------------------------------

package model;

import static  model.FactoryPattern.whichObjectCreate;

public class Report {
  
    public static void main(String[] args) {
      int pdfReport=1;
      int htmlReport=2;
      int excelReport=3;
      int xmlReport=4;
      int textReport=5;
      whichObjectCreate(pdfReport).reportType();
      whichObjectCreate(htmlReport).reportType();
      whichObjectCreate(excelReport).reportType();
      whichObjectCreate(xmlReport).reportType();
      whichObjectCreate(textReport).reportType();
    
    }
  
}
-------------------------------------------------------------------------------------------------------------
//FactoryPattern  Class
-------------------------------------------------------------------------------------------------------------
package model;

public abstract  class FactoryPattern {
   
   public static FactoryPattern whichObjectCreate(int reportType){
     switch(reportType){
      /*Case One  for HTML Report*/
       case 1: return new HTMLReport();
      /*Case Two  for PDF Report*/
       case 2: return new PDFReport();
      /*Case Three  for Excle Report*/
       case 3: return new ExcelRepot();
      /*Case Four  for XML Report*/
       case 4: return new XMLReport();
     }
     /*Default   for Text Report*/
     return new TextReport();
   }
   /*abstract method impelement by extends class */
   public abstract void  reportType();
 }
------------------------------------------------------------------------------------------------------------
//TextReport class
------------------------------------------------------------------------------------------------------------
package model;

public class TextReport extends FactoryPattern{
    public void reportType(){
      System.out.println("This Method is Generator TextReport");
    }
}
------------------------------------------------------------------------------------------------------------
//ExcelRepot class
------------------------------------------------------------------------------------------------------------
package model;

public class ExcelRepot extends FactoryPattern{
  public void  reportType(){
    System.out.println("This Method is Generator ExcleRepot");
  }
}

------------------------------------------------------------------------------------------------------------
//HTMLReport class
------------------------------------------------------------------------------------------------------------
package model;

public class HTMLReport extends FactoryPattern {
    
  public void  reportType(){
    System.out.println("This Method is Generator HTMLReport");
  }
    
}
------------------------------------------------------------------------------------------------------------
//PDFReport class
------------------------------------------------------------------------------------------------------------
package model;

public class PDFReport extends FactoryPattern{
  public void  reportType(){
    System.out.println("This Method is Generator PDFReport");
  }
    
}
------------------------------------------------------------------------------------------------------------
//XMLReport class
------------------------------------------------------------------------------------------------------------
package model;

public class XMLReport extends FactoryPattern {
    
  public void  reportType(){
    System.out.println("This Method is Generator XMLReport");
  }
}