Design Patterns (Part 1) : Factory pattern






Design patterns represent the best practices used by experienced object-oriented software developers. Design patterns are solutions to general problems that software developers faced . These solutions were obtained by trial and error by numerous software developers over quite a substantial period of time. This tutorial will take you through step by step approach and examples using Java while learning Design Pattern concepts.



Factory pattern

Factory pattern is one of most used design pattern in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object.
In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface.
We're going to create a Human interface and concrete classes implementing the Human interface. A factory class HumanFactory is defined as a next step.
our TutorialMain class will use HumanFactory to get a Humanobject. It will pass information (DOCTOR / ENGINEER / LAWYER) to HumanFactory to get the type of object it needs.

Step 1

Create an interface.
 

public interface Human {
   void job();
}
 


Step 2

Create concrete classes implementing the same interface.
 

public class Doctor implements Human {

   @Override
   public void job() {
      System.out.println("Hi I'm Dr");
   }
}
 
 

public class Engineer implements Human {

   @Override
   public void job() {
      System.out.println("Hi I'm eng");
   }
}
 
 

public class Lawyer implements Human {

   @Override
   public void job() {
      System.out.println("Hi I'm lawyer");
   }
}
 


Step 3

Create a Factory to generate object of concrete class based on given information.
 

public class HumanFactory {
  
   //use getHuman method to get object of type human 
   public Human getHuman(String HumanJob){
      if(HumanJob == null){
         return null;
      }  
      if(HumanJob.equalsIgnoreCase("DOCTOR")){
         return new Doctor();
         
      } else if(HumanJob.equalsIgnoreCase("ENGINEER")){
         return new Engineer();
         
      } else if(HumanJob.equalsIgnoreCase("LAWYER")){
         return new Lawyer();
      }
      
      return null;
   }
}
 


Step 4

Use the Factory to get object of concrete class by passing an information such as type.
 

public class TutorialMain {

   public static void main(String[] args) {
      HumanFactory humanFactory = new HumanFactory();

      //get an object of Doctor and call its job method.
      Human Human1 = humanFactory.getHuman("DOCTOR");

      //call job method of Doctor
      Human1.job();

      //get an object of Engineer and call its job method.
      Human Human2 = humanFactory.getHuman("ENGINEER");

      //call job method of Engineer
      Human2.job();

      //get an object of Lawyer and call its job method.
      Human Human3 = humanFactory.getHuman("LAWYER");

      //call job method of Lawyer
      Human3.job();
   }
}
 


Output

Hi I'm Dr
Hi I'm eng
Hi I'm lawyer
Previous
Next Post »

1 comments:

Write comments
Unknown
AUTHOR
September 21, 2017 at 7:11 AM delete

"I very much enjoyed this article.Nice article thanks for given this information. i hope it useful to many pepole.php jobs in hyderabad.
"

Reply
avatar