Factory Method Pattern - Walking Techie

Blog about Java programming, Design Pattern, and Data Structure.

Thursday, July 7, 2016

Factory Method Pattern

Factory pattern or Factory method pattern define an interface or abstract class to create an object, but let the subclasses decide which class to instantiate. In other word subclasses are responsible to create the instance on class.

In this pattern, we create object without exposing creation logic to the client. A superclass specifies all standard and generic behavior , and then delegates the creation details to subclasses that are supplied by the client.

Factory method pattern provides loose coupling and high cohesion. Factory pattern encapsulate object creation logic which make it easy to change it later when you change how object get created or you can introduce new object with just change in one class.

Implementation

We are going to create Vehicle interface. TwoWheeler, ThreeWheeler, and FourWheeler classes implementing Vehicle interface. A factory class VehicleFactory that will give object corresponding the input value.

UML Diagram: Factory Method Pattern UML
public interface Vehicle {
 void printVehicle();
}
public class TwoWheeler implements Vehicle {

 @Override
 public void printVehicle() {
  System.out.println("I am two wheeler");
 }
}
public class ThreeWheeler implements Vehicle {

 @Override
 public void printVehicle() {
  System.out.println("I am three wheeler");
 }
}
public class FourWheeler implements Vehicle {

 @Override
 public void printVehicle() {
  System.out.println("I am four wheeler");
 }
}
Factory class to generate object of concrete class based on providing information.
public class VehicleFactory {
 public Vehicle getVehicle(String vName) {
  if (vName == null)
   return null;
  else if (vName.equalsIgnoreCase("TwoWheeler"))
   return new TwoWheeler();
  else if (vName.equalsIgnoreCase("ThreeWheeler"))
   return new ThreeWheeler();
  else if (vName.equalsIgnoreCase("FourWheeler"))
   return new FourWheeler();
  else
   return null;
 }
}
public class Client {

 public static void main(String[] args) {
  VehicleFactory factory = new VehicleFactory();
  // get an object of TwoWheeler and call printVehicle
  Vehicle vehicle = factory.getVehicle("TwoWheeler");
  vehicle.printVehicle();  //print I am two wheeler

  // get an object of ThreeWheeler and call printVehicle
  vehicle = factory.getVehicle("ThreeWheeler");
  vehicle.printVehicle(); //print I am three wheeler

  // get an object of FourWheeler and call printVehicle
  vehicle = factory.getVehicle("FourWheeler");
  vehicle.printVehicle(); //print I am four wheeler
 }
}
/* Output of the above program.
I am two wheeler
I am three wheeler
I am four wheeler*/

In the above example, we have totally decoupled the selection of type for object creation from Client. The library is now responsible to decide which object type to create based on an input. Client just needs to make call to library’s factory getVehicle() method and pass the vName it wants without worrying about the actual implementation of creation of objects.

1 comment :

  1. factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
    eehhaaa This newsletter from Jaalifestyle: As we all know, we need a total of 1.5 million people to start an advertising program.
    sniffies Sniffies isn’t bookmarked on my phone, however I’ve been logged in enough that the web target will auto-complete on my personal browser.
    college-party-guide One of the most important rituals for students in college is to throw the most memorable student dorm celebration.

    ReplyDelete