Sunday, 26 November 2017

Algorithms | Factory Method Pattern (Virtual Constructor)

Problem statement
Consider a scenario, you are using multiple database servers like SQL Server and Oracle and developing an application using SQL Server database as a backend, but in the future need to change backend database to Oracle. In that case, you need to modify all code, if you haven’t written down code using factory design pattern.

In factory pattern, you need to do very small change work to achieve above-mentioned functionality. A class implementing factory method pattern takes to care for the change. You just need to make small changes in the configuration to switch from the database server to another.

Intent
1. Define an interface for creating objects, but let subclasses to decide which class to instantiate.
2. Refer to the newly created object through a common interface.

When we want to return one sub-class object from multiple sub-classes using an input, should use Factory design pattern. Factory class takes responsibility of instantiation the class (We can return Singleton instance from static factory method as well).

In Factory pattern, we create an object without exposing the creation logic to the client and refer to a newly created object using a common interface.
  


Example
Coffee/Vending machine, give input using options and as per input coffee, lemon tea, plain milk or hot water will be an output.

interface Drink {
       void prepare();
}

class Coffee implements Drink {
       @Override
       public void prepare() {
              System.out.println("Coffee is prepared !!");
       }
}

class LemonTea implements Drink {
       @Override
       public void prepare() {
              System.out.println("Lemon Tea is prepared !!");
       }
}

class PlainWater implements Drink {
       @Override
       public void prepare() {
              System.out.println("Plain Water is prepared !!");
       }
}

class VedingMachine {
       public static Drink getDrink(String str) {
              if("PlainWater".equals(str)) {
                     return new PlainWater();
              } else if("Coffee".equals(str)) {
                     return new Coffee();
              } else if("LemonTea".equals(str)) {
                     return new LemonTea();
              }
              return null;
       }
}

public class FactoryPatternTest {
       public static void main(String[] args) {
              Drink drink = VedingMachine.getDrink("Coffee");
              drink.prepare();
       }
}

Output: Coffee is prepared!!

Benefits of Factory Method Pattern
Factory Method Pattern provides an approach to code for interface rather than implementation and it provides the abstraction between implementation and client classes through inheritance.

Factory Method Pattern allows the sub-classes to choose the type of objects to create.

We can easily change the implementation of sub-class because client program is unaware of this. It makes the code more robust, less coupled and easy to extend (client interacts solely with the resultant interface or abstract class).

Usage in JDK
java.util.Calendar, ResourceBundle, and NumberFormat getInstance() methods use Factory pattern.

valueOf() method in wrapper classes like Boolean, Integer etc.

Spring and Hibernate frameworks.


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...