Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Java Design Patterns Interview Questions and Answers

Question: What is Factory Design Pattern with example?
Answer:

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 Mobile interface and concrete classes implementing the Mobile interface. A factory class MobileFactory is defined as a next step.

FactoryPatternDemo, our demo class will use MobileFactory to get a Mobile object. It will pass information (SAMSUNG / SONY / BLACKBERRY) to MobileFactory to get the type of object it needs.

Step 1: Create an interface.

Mobile.java

public interface Mobile {

   void draw();

}


Step 2

Create concrete classes implementing the same interface.

Sony.java

public class Sony implements Mobile {

   @Override

   public void draw() {

      System.out.println("Inside Sony::draw() method.");

   }

}


Blackberry.java

public class Blackberry implements Mobile {

   @Override

   public void draw() {

      System.out.println("Inside Blackberry::draw() method.");

   }

}


Samsung.java

public class Samsung implements Mobile {

   @Override

   public void draw() {

      System.out.println("Inside Samsung::draw() method.");

   }

}


Step 3

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

MobileFactory.java

public class MobileFactory {

   //use getMobile method to get object of type Mobile 

   public Mobile getMobile(String MobileType){

      if(MobileType == null){

         return null;

      }

      if(MobileType.equalsIgnoreCase("SAMSUNG")){

         return new Samsung();

      } else if(MobileType.equalsIgnoreCase("SONY")){

         return new Sony();

      } else if(MobileType.equalsIgnoreCase("BLACKBERRY")){

         return new Blackberry();

      }

      return null;

   }

}


Step 4

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

FactoryPatternDemo.java

public class FactoryPatternDemo {

   public static void main(String[] args) {

      MobileFactory MobileFactory = new MobileFactory();

      //get an object of Samsung and call its draw method.

      Mobile Mobile1 = MobileFactory.getMobile("SAMSUNG");


      //call draw method of Samsung

      Mobile1.draw();


      //get an object of Sony and call its draw method.

      Mobile Mobile2 = MobileFactory.getMobile("SONY");


      //call draw method of Sony

      Mobile2.draw();


      //get an object of Blackberry and call its draw method.

      Mobile Mobile3 = MobileFactory.getMobile("BLACKBERRY");


      //call draw method of Blackberry

      Mobile3.draw();

   }

}


Step 5

Verify the output.

Inside Samsung::draw() method.

Inside Sony::draw() method.

Inside Blackberry::draw() method.

Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook