Sunday, 6 November 2016

Java Source World: Factory Pattern or Factory Method Pattern

Factory Pattern or Factory Method Pattern | Java Source World

                                                                             

                                                                           Java Source World      A Java quick reference blog


Factory Pattern (Known as the factory method pattern):




It is a creational design pattern.


A factory is a java class that is used to encapsulate object creation code.

A factory class instantiates and returns a particular type of Object based on data passed to the factory.

The different types of Objects that are returned from a factory typically are subclasses of a common parent class.

The data passed from the calling code to the factory can be passed either when the factory is created or when the method on the factory is called to create an object. This created method is often called something such as getInstance or getClass.

Example:  

Lets create an product factory class that will return a product object based on the source type of data input.

To start,here is an  Product interface.The factory will return an instantiated subclass of product. 

Product has an abstract method, createProduct().



Product.java

package com.javaconcept.designpattern.factory;

public interface Product {
      String createProduct();
}



Airline.java

package com.javaconcept.designpattern.factory;

public class Airline implements Product{

      @Override
      public String createProduct() {
            return "Airline";
      }

}


Logistics.java

package com.javaconcept.designpattern.factory;

public class Logistics implements Product{

      @Override
      public String createProduct() {
            return "Logistics";
      }

}



Above we have, Airline.java and Logistics.java are the two interfaces and represents two products.

Now let us implement our factory.

We will call our factory's object creation method createProduct(). This method takes string as a parameter. If the String is "A" it returns "Airline" object, or if the string is passed as "L" it returns "Logistics" object.



ProductFactory.java

package com.javaconcept.designpattern.factory;

public class ProductFactory {
      public static Product createProduct(String productType){
            if("A".equals(productType)){
                  return new Airline();
            }else if("L".equals(productType)){
                  return new Logistics();
            }
            return null;
      }
}

FactoryTest.java

package com.javaconcept.designpattern.factory;

public class FactoryTest {
      public static void main(String[] args) {
            Product p1 = ProductFactory.createProduct("A");
            System.out.println(p1.createProduct());     
                  Product p2 = ProductFactory.createProduct("L");
                  System.out.println(p2.createProduct());
      }
}    




From the above example we can understand that:

Factory Design pattern is based on Encapsulation object oriented concept. 

Factory method is used to create different object from factory often referred as item and it encapsulate the creation code. So instead of having object creation code on client side we encapsulate inside Factory method in java


When to use Factory design pattern in java:

  • Static Factory methods are common in frameworks where library code needs to create objects of types which may be sub classed by applications using the framework.

  • Some or all concrete products can be created in multiple ways, or we want to leave open the option that in the future there may be new ways to create the concrete product.

  • Factory method is used when Products don't need to know how they are created.

  • We can use factory pattern where we have to create an object of any one sub-classes depending on the data provided.

Other posts you may like to read:
OOPs concepts in java



                                                                                                                    Java Source World      A Java quick reference blog




No comments:

Post a Comment