Saturday 29 May 2010

Factory Method

Factory Method or Simple Factory Method


  • The method is capable of creating and returning either its own class object or some other class object is called Factory Method.
Advantages:

  • to create object of java class out side the class when class is having only private constructors
  • to centralise object creation process in single method and to apply restriction number of object creation
  • to create new object based on existing object and its data
  • to develop immutable java class
Rules to use Factory method


  • this method should return one or other class object. If return type is abstract class or interface, the method must return one or other implementation of that abstract class or interface
  • The Factory method should not return primitive data type or void
  • method defination should have logic of creating and returning a class object 
  • factory method can be a static factory method or an instance factory method.
  •  

Types of Factory Method

Static factory methods
Instance factory method

Static factory methods:


  • it is useful to create an object of a java class outside of that class when that class is having only private constructor or you want to create a new object based on current status of the existing object.
Example:
  1.   Class c=Class.forName(...);// new object
  2. Thread t=Thread.currentThread();//based on Current Status
  3. Toolkit tk=Toolkit.getDefaultToolkit();//new object
java class have Factory methods even though that class is having public constructors.

Instance factory method


  • it is useful to create a new object for java class by using existing object and its data.
Ex:
for pre defined instance factory method is:

String s1="Good morning";//existing object
String s2=s1.substring(3,7);//new object i.e: it gives come

Class c=Class.forName("Demo");//static factory method returning Class object
Object ob=c.newInstance();//non-static factory method returning Demo class object wrapped inside Object class object

Example program on Factory method
//by using Static factory method and instance factory method
package com.raj;

public class Employee {
int empId;
String empName;
float empSalary;

//private zero argument constructor
private Employee()
{
System.out.println("Default constructor");
}
//static factory method
public static Employee createNewEmployee(int empId,String empName)
{
//use Class.forName ()
Employee emp=new Employee();
emp.empId=empId;
emp.empSalary=6000;//default salary of new employee
emp.empName=empName;
return(emp);
} //end of static factory method
//instance factory method
//To increment salary, we need existing employee object
public Employee incrementSalary(float hike)
{
Employee emp=new Employee();
emp.empSalary=this.empSalary*(1+hike);//20% hike
//copy all the necessary properties
emp.empId=this.empId;
emp.empName=this.empName;
return(emp);
}
public String toString()
{
return("EmpDetails="+empId+":"+empName+":"+empSalary);
}

} //end of Employee class

package com.raj;

public class TestClient {
public static void main(String args[]) throws Exception
{
Employee emp1=Employee.createNewEmployee(1001, "Rama");
//emp1 calls toString()method internally
System.out.println(emp1);

Employee emp2=emp1.incrementSalary(0.20f);
System.out.println("With Hike"+emp2);
}//main

}//end of TestClient class
/* output
 * Default constructor
EmpDetails=1001:Rama:6000.0
Default constructor
With HikeEmpDetails=1001:Rama:7200.0005

 */
*/

No comments:

Post a Comment