Showing posts with label Factory Pattern. Show all posts
Showing posts with label Factory Pattern. Show all posts

Simple Factory Design Pattern class

Lets design simple Factory Design Pattern example with code, like how to design a Factory class and how to get objects from factory. 

Consider the scenario that showroom needs application to access their various Car details through online. Like Car1 runs on Petrol and gives mileage upto 28 KM/L and Car2 runs on Diesel and gives mileage upto 24 KM/L. 
Lets create simple Factory Class to create car objects based on parameter information's.

AANND



public interface Car {
 public String getCallDetails();
}



public class Alto implements Car{

 public String getCallDetails() {
  return "Alto runs on Petrol and gives mileage upto 18 KM/L";
 }
}



public class Swift implements Car{

 public String getCallDetails() {
  return "Swift runs on Diesel and gives mileage upto 24 KM/L";
 }
}



public class CarFactory {

 public static Car getCar(String carName){
  
  Car car = null;
  
  if(carName.equalsIgnoreCase("alto")){
   car = new Alto();
  
  }else if(carName.equalsIgnoreCase("swift")){
   car = new Swift();    
  }
  
  return car;
 }
}



public class ShowRoomApplication {

 public static void main(String[] args) {
  
  Car car1 = CarFactory.getCar("alto");
  String details = car1.getCallDetails();
  System.out.println(details);
  
  Car car2 = CarFactory.getCar("swift");
  details = car2.getCallDetails();
  System.out.println(details);
  
  
 }
}



Output:


Alto runs on Petrol and gives mileage upto 28 KM/L
Swift runs on Diesel and gives mileage upto 24 KM/L

How to create Factory Design Pattern in Java

Already we have seen Fully Singleton Design Pattern in our earlier tutorial, now we will see about Factory Design Pattern. 

Factory design pattern is one is the most important and widely used pattern every where in Java. The factory method pattern defines an interface for creating an object, but let sub-classes decide which class to instantiate based on the user requirement. JDK and most of all frameworks like Spring, Struts etc uses factory design pattern internally. Also factory design pattern can be classified into various types like Static Factory, Service Locator Factory and Abstract Factory. 

Lets see one simple example as getting various Computer instances based on the used requirement. As we already explained above we need to have a interface for creating and Object but sub-classes will decide which class need to be instantiated  As same way we are going to have interface called "Computer" and sub-classes like "Desktop", "WorkStation", "Server", "Laptop" and "SuperComputer" will implement "Computer" interface and will their own method definitions. 



public interface Computer {
 public String myComputerType();
}


public class Desktop implements Computer {
 public String myComputerType() {
  return "You have requested for Desktop";
 }
}


public class WorkStation implements Computer {
 public String myComputerType() {
  return "You have requested for WorkStation";
 }
}


public class Server implements Computer {
 public String myComputerType() {
  return "You have requested for Server";
 }
}


public class Laptop implements Computer {
 public String myComputerType() {
  return "You have requested for Laptop";
 }
}


public class SuperComputer implements Computer {
 public String myComputerType() {
  return "You have requested for SuperComputer";
 }
}



public public class FactoryPattern {
 
 public static void main(String[] args) {

  // Based on user requirement we will get the Object
  
  Computer obj = FactoryPattern.getComputerType("desktop");
  if(obj != null) 
   System.out.println(obj.myComputerType());
  
  obj = FactoryPattern.getComputerType("laptop");
  if(obj != null) 
   System.out.println(obj.myComputerType());
  
  obj = FactoryPattern.getComputerType("server");
  if(obj != null) 
   System.out.println(obj.myComputerType());
 }
 
 public static Computer getComputerType(String val){
  if(val.equalsIgnoreCase("desktop")) return new PC();
  else if(val.equalsIgnoreCase("workstation")) return new WorkStation();
  else if(val.equalsIgnoreCase("server")) return new Server();
  else if(val.equalsIgnoreCase("laptop")) return new Laptop();
  else if(val.equalsIgnoreCase("super")) return new SuperComputer();
  return null;
 }
}



OUTPUT:


You have requested for Desktop
You have requested for Laptop
You have requested for Server