SEARCH BY

All

  • All
  • Java8
  • Spring

Factory method pattern in java

Post a Comment

Introduction

Factory method pattern is one of the Creational Design Pattern in java. In this design pattern super class has sub classes , and sub classes will be instantiated via Factory class only. That means class will be initiated with factory methods only, all classes initiated via factory class must belongs to same family ( that means all subclass are must has same super class ).

Factory pattern will change the way of object creation at client class. At client class we can not use new operator directly to create an object for subclasses, that means we can create objects for subclass by using factory class only.

Key Components in Factory method pattern

There are different components( classes ) are existing while creating application with Factory method design patter. Following are components

  • Super Class ( generally it is an abstract class in most of the cases , and even super class can be interface or normal class also )
  • Sub classes to super class ( there may be more than one sub class and implements super class abstract methods)
  • Factory Class ( which has a factory method to produce objects of sub class based on given input to the method )
  • Client class ( this is the class where we are going to create an objects fro sub class by using factory method created in factory class )
Factory Method Pattern Architecture
Factory Method Pattern Architecture 

Factory Method Pattern Example

For implementing factory method pattern example iam going to develop a super class named as Curry.java and three sub classes named like Veg.java, NonVeg.java and Chinese.java. In super class we have two methods where one method is concrete method and another one is abstract method which is to be implemented in sub classes. First we will see the code for these super and sub classes then we will create factory class with factory method finally ends with client class creation.
public abstract class Curry {

 public abstract String getCurryDetails();

 public void TotalMenu() {

  System.out.println("Curries available in this menu \n ***********************");

  System.out.println("VEG | NONVEG | CHINESE \n ********************************");
 }
}
public class Veg extends Curry {

 @Override
 public String getCurryDetails() {
  // TODO Auto-generated method stub
  return "Thanks for choosing veg curry";
 }

}
public class NonVeg extends Curry {

 @Override
 public String getCurryDetails() {
  // TODO Auto-generated method stub
  return "Thanks for choosing non veg curry";
 }

}
public class Chinese extends Curry {

 @Override
 public String getCurryDetails() {
  // TODO Auto-generated method stub
  return "Thansks for choosing Chinese curry";
 }

}
public class CurryFactory {

 // create a reference to super class
 Curry curry;

 public Curry getCurryFacttoryMethod(String ctype) {

  switch (ctype) {
  case "VEG":
   curry = new Veg();
   break;
  case "NONVEG":
   curry = new NonVeg();
   break;
  case "CHINESE":
   curry = new Chinese();
   break;
  default:
   break;
  }

  return curry;
 }

}
public class Client {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  // create an object for CurryFactory class
  CurryFactory cFactory = new CurryFactory();
  // to get veg object
  Curry c = cFactory.getCurryFacttoryMethod("VEG");
  c.TotalMenu();
  String veg = c.getCurryDetails();
  // get nonveg object

  c = cFactory.getCurryFacttoryMethod("NONVEG");
  String nonveg = c.getCurryDetails();

  // get chinese object

  c = cFactory.getCurryFacttoryMethod("CHINESE");
  String chinese = c.getCurryDetails();
  System.out.println(veg + "\n ---------------------- ");
  System.out.println(nonveg + "\n -----------------------");
  System.out.println(chinese + "\n ----------------------");

 }

}
Curries available in this menu 
 ***********************
VEG | NONVEG | CHINESE 
 ********************************
Thanks for choosing veg curry
 ---------------------- 
Thanks for choosing non veg curry
 -----------------------
Thansks for choosing Chinese curry
 ----------------------
jaya
I love software designing, coding, writing and teaching...

Share this article

Related Posts

Post a Comment

Place your code in <em> </em> tags to send in comments | Do not add hyper links in commnet

Close

Subscribe our newsletter for the latest articles directly into your email inbox