» » » Factory Method Pattern


Factory Method Pattern:

  • Problem: Using new keyword we can not create object with flexibility and by applying restrictions.
  • Solution: Use Factory pattern (or) Factory method.
  • By defining a abstract class or an interface but let the subclass  decide which class object to instantiate.
  • A method of a class capable of constructing and returning its own class object or other class object is called "factory method".
  • There are two types of factory methods.
  1. Static factory method.
  2. Instance factory method.

 1.Static Factory method:

  • A static method defined to construct and return object of same class or different is known as static factory method.
  • Some of the pre defined static factory methods are as follows.
  1. Thread th= Thread.currentThread();
  2. Class c=Class.forName();
  3. Runtime rt=Runtime.getRuntime();
  4. Calendar c=Calendar.getInstance();

2.Instance Factory method:

  • A non static method defined to construct and return object of same class or different is known as instance factory method.
  • Some of the pre defined instance factory methods are as follows.
  1.  String s= new String("instance of");
     String s1=s.concat("java");
  2. StringBuffer sb=new StringBuffer("instance of");
    sb=sb.subString(0,2);
  3. Date d= new Date();
    String s=d.toString();
Program on Factory method / Factory Design pattern:

Step 1: create an interface:

  1. package com.instanceofjavaforus;
  2.  
  3. public interface Vehicle {
  4.  
  5.  public void engine();
  6.  
  7. }
     

 Step 2: create a Two Wheeler class which implements vehicle interface:

  1. package com.instanceofjavaforus;
  2.  public class TwoWheeler implements Vehicle {
  3.  
  4.    @Override
  5.     public void engine() {
  6.         System.out.println("Two Wheeler");
  7.     }
  8.  
  9. }

Step 3:: create a Four Wheeler class which implements vehicle interface:

  1. package com.instanceofjavaforus;
  2.  public class FourWheeler implements Vehicle {
  3.  
  4.    @Override
  5.     public void engine() {
  6.         System.out.println("FourWheeler");
  7.     }
  8.  
  9. }

Step 4: create a SixWheeler class which implements vehicle interface:

  1. package com.instanceofjavaforus;
  2.  public class SixWheeler implements Vehicle {
  3.  
  4.    @Override
  5.     public void engine() {
  6.         System.out.println("SixWheeler");
  7.     }
  8.  
  9. }

Step 5: create a TestEngine class which is having factory method:

  1. package com.instanceofjavaforus;
  2.  public class TestEngine {
  3.  public Vehicle getVehicle(String venname){
  4.      if(venname==null){
  5.             return null;
  6.        }else if(venname.equalsIgnoreCase("TwoWheeler")){
  7.             return new TwoWheeler();
  8.         }else if(venname.equalsIgnoreCase("FourWheeler")){
  9.             return new FourWheeler();
  10.         }else if(venname.equalsIgnoreCase("SixWheeler")){
  11.             return new SixWheeler();
  12.         }
  13.     
  14.         return null;
  15.     }
  16. }

Step 6: create a FactoryDemo class:

  1. package com.instanceofjavaforus;
  2.  public class FactoryDemo {
  3.   public static void main(String args[]){
  4.  
  5.      TestEngine te= new TestEngine();
  6.  
  7.         Vehicle vehcle1=te.getVehicle("TwoWheeler");
  8.        vehcle1.engine();
  9.  
  10.         Vehicle vehcle2=te.getVehicle("FourWheeler");
  11.         vehcle2.engine();
  12.  
  13.         Vehicle vehcle3=te.getVehicle("SixWheeler");
  14.          vehcle3.engine();
  15.  }
  16. }

  1. OutPut:
  2. TwoWheeler
  3. FourWheeler
  4. SixWheeler

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

No comments

Leave a Reply

Select Menu