0 votes
in Design Patterns by
What is a Factory Design Pattern?

1 Answer

0 votes
by

Factory design pattern belongs to the category of Creational Design Patterns. Here, the objects are created without exposing the logic of creation to the client. The objects refer to the common interface.

Let us understand this with the help of an example.

Let’s consider 3 classes Square, Rectangle and Triangle. We will be using factory patterns to create objects of these three classes without exposing the creation logic by making use of ShapeFactory class. The Driver class would be passing the information like RECTANGLE/SQUARE/TRIANGLE for getting the required object. The following UML diagram represents the scenario.

Now to implement the factory design pattern for the above example, let us follow the below steps:

Step 1: Create a Shape interface.

   //Shape.java

   public interface Shape {

      void draw();

   }

Step 2: Create concrete classes Rectangle, Square, Triangle that implements the Shape interface.

   //Rectangle.java

   public class Rectangle implements Shape {

      @Override

      public void draw() {

         System.out.println("Rectangle Drawn");

      }

   }

   //Square.java

   public class Square implements Shape {

      @Override

      public void draw() {

         System.out.println("Square Drawn");

      }

   }

   //Triangle.java

   public class Triangle implements Shape {

      @Override

      public void draw() {

         System.out.println("Triangle Drawn");

      }

   }

Step 3: Create ShapeFactory class and create a method called getShapeInstance() for generating objects of the concrete classes defined above.

   //ShapeFactory.java

   public class ShapeFactory {

      //the method will be used to get object of required shape

      public Shape getShapeInstance(String type){

         if(type == null){

            return null;

         } 

         if(type.equalsIgnoreCase("TRIANGLE")){

            return new Triangle();

         } else if(type.equalsIgnoreCase("SQUARE")){

            return new Square();

         } else if(type.equalsIgnoreCase("RECTANGLE")){

            return new Rectangle();

         }

         return null;

      }

   }

Step 4: Implement the Driver class and utilise the factory class for getting the object of the required type.

   //Driver.java

   public class Driver {

      public static void main(String[] args) {

         ShapeFactory shapeFactory = new ShapeFactory();

         //get Triangle object and call draw()

         Shape triangle = shapeFactory.getShape("Triangle");

         triangle.draw();

         //get Rectangle object and call draw()

         Shape rectangle = shapeFactory.getShape("RECTANGLE");

         rectangle.draw();

         //get Square object and call draw()

         Shape square = shapeFactory.getShape("SQUARE");

         square.draw();

      }

   }

Step 5: Validate the output

The output of this implementation would be:

  Triangle Drawn

  Rectangle Drawn

  Square Drawn

The advantages of a factory design pattern are:

This pattern allows hiding the creation logic of the application by using interfaces and factory classes.

It lets to test the seamlessness of the application by using mock or stubs.

Related questions

+1 vote
asked Oct 16, 2019 in Design Patterns by DavidAnderson
0 votes
asked Oct 17, 2019 in Design Patterns by Robin
...