Program to demonstrate how to implement Factory Design Pattern in Java
In order to implement Factory Design Pattern, we will create following classes/interface and properties file :-
Step 1 : Create a Interface "Animal"
Step 2 : Create a concrete implementation of Animal interface as "Dog"
Step 3 : Create a concrete implementation of Animal interface as "Cat"
Step 4 : Create a properties file which reads for the dynamic class name
Step 5 : Create a Factory class which produces Animal object based on properties file
Step 6 : Write a Test class to run the application.
Output of the program :
In order to implement Factory Design Pattern, we will create following classes/interface and properties file :-
Step 1 : Create a Interface "Animal"
package com.hubberspot.designpatterns.factory.example;
// Step 1 : Create a Simple interface for polymorphic
// behavior ... here it is Animal having two simple
// method as eat() and sleep()
public interface Animal {
public void eat();
public void sleep();
}
Step 2 : Create a concrete implementation of Animal interface as "Dog"
package com.hubberspot.designpatterns.factory.example;
// Step 2 : create object of Animal say Dog
// implementing Animal interface.
// Provide concrete implementation to the
// two methods eat() and sleep() respective to Dog()
public class Dog implements Animal{
@Override
public void eat() {
System.out.println("Dog : eat()");
}
@Override
public void sleep() {
System.out.println("Dog : sleep()");
}
}
Step 3 : Create a concrete implementation of Animal interface as "Cat"
package com.hubberspot.designpatterns.factory.example;
//Step 3 : create object of Animal say Cat
//implementing Animal interface.
//Provide concrete implementation to the
//two methods eat() and sleep() respective to Cat
public class Cat implements Animal
{
@Override
public void eat() {
System.out.println("Cat : eat()");
}
@Override
public void sleep() {
System.out.println("Cat : sleep()");
}
}
Step 4 : Create a properties file which reads for the dynamic class name
# Step 4 : Create a configuration file which will provide us with # name of Concrete Animal class at the runtime. #Animals Configurations animal.classname=com.hubberspot.designpatterns.factory.example.Dog
Step 5 : Create a Factory class which produces Animal object based on properties file
package com.hubberspot.designpatterns.factory.example;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.PropertyResourceBundle;
// Step 5 : Create a class which will act as a factory for
// creating concrete objects of Animal
public class AnimalFactory {
// Providing the name of the resource files from where the
// properties will be read
private static final String ANIMALS_PROP = "animal.properties";
private Class concreteClass ;
public AnimalFactory()
{
FileInputStream propInp = null;
try {
// Creating a FileInputStream by passing it the name of the
// resource
propInp = new FileInputStream(ANIMALS_PROP);
// PropertyResourceBundle class takes in the resource file
// as a resource bundle
PropertyResourceBundle bundle = new PropertyResourceBundle(propInp);
// Taking the name of class from the resource file
String className = bundle.getString("animal.classname");
// Class.forName() method will dynamically load the class
// passed to it as a parameter
concreteClass = Class.forName(className);
}catch(Exception ex){
}finally {
try {
// closing the FileInputStream
propInp.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// This method will act as a method that will work as a factory
// and will return the object asked based on the configuration
// files entry.
public Animal createAnimal(){
try {
// Returns concrete implementation of Animal by
// calling newInstance() method on the Class instance
// Here in our case properties file points to Dog object
// so based on the resource file the object will be created .
return (Animal)concreteClass.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
Step 6 : Write a Test class to run the application.
package com.hubberspot.designpatterns.factory.example;
public class Test {
public static void main(String[] args) {
// Create a AnimalFactory Object and call its createAnimal method
// This method will read the name of the class from the
// properties file and returns back the Object of that class
// dynamically.
// Now if we want to create object of Cat object, we just need to
// change properties file thats it . Our code remains untouched
AnimalFactory animalFactory = new AnimalFactory();
Animal animal = animalFactory.createAnimal();
// Calling the respective methods
animal.eat();
animal.sleep();
}
}
Output of the program :
