Factory Method: Creational design patterns

0
76
Factory Method: Creational design patterns

You must read the following article before going through this article.

See More:
Why Design Patterns important in Java
When you build software, you have to face lots of...

In this tutorial, I will explain the Factory method design pattern. Let say we have an Interface and we have multiple implementations of the same interface. For example,

We have an Interface “Car”. We have implementations as “Honda”, “Maruti” and “Mercedes”. If you want to create an object or instantiate “Car”, You have to use one of the cars, either you can use “Honda” or “Maruti” or “Mercedes”.

Now how to mention that which one you want. Depends on your requirement, you will create an object of the different classes, Maybe Honda or Maruti or Mercedes.

But the main thing is, you want to hide the logic of the object-creation from the user.

We can do it by Factory Method Design pattern.

Let see by coding:

File structure:

Car.java

package co.tellmehow.dp;

public interface Car {
  
  void drive();

}

 

This is the Car interface. It will be implemented by Honda, Maruti, and Mercedes classes.

Honda.java

package co.tellmehow.dp;
public class Honda implements Car {

  @Override
  public void drive() {
    
    System.out.println("I am driving Honda");
    
  }

}

Maruti.java

package co.tellmehow.dp;
public class Maruti implements Car {

  @Override
  public void drive() {
    
    System.out.println("I am driving Maruti");
    
  }

}

 

Mercedese.java

package co.tellmehow.dp;

public class Mercedese implements Car {

  @Override
  public void drive() {
    
    System.out.println("I am driving Mercedes");
    
  }

}

 

Suppose I want to drive Mercedes. It’s very simple. We can create an object of Mercedes.

Car car = new Mercedese();
car.drive();

Similarly, we can create objects for other cars. But this is not good practices.

Our approach should be to hide object creation from users. We should create a class where object creation login will work. We can say this class Factory.

Let’s create a Factory class.

CarFactory.java

package co.tellmehow.factory;

import co.tellmehow.dp.Car;
import co.tellmehow.dp.Honda;
import co.tellmehow.dp.Maruti;
import co.tellmehow.dp.Mercedese;


public class CarFactory {
  
  public Car getCar(String str) {
    
    if(str.equals("mostluxury")) {
      return new Mercedese();
    }
    if(str.equals("luxury")) {
      return new Honda();
    }
    else {
      return new Maruti();
    }
        
  }

}

Explanation:

Here I have created a method of “Car” type return. Now, If I create an object of CarFactory class and call “getCar” method.

CarFactory cf = new CarFactory();

Now, I create an object of a car. If you want to create an object of Honda. We just pass “luxury” parameter to getCar method of CarFactory.

For Mercedese object, pass “mostluxury” parameter.

Car car = cf.getCar(“luxury”); // create object of Honda

Lets see it in main class.

CarApp.java

package co.tellmehow.dp;

import co.tellmehow.factory.CarFactory;

public class CarApp {
  
  public static void main(String[] args) {
    
    CarFactory cf = new CarFactory();
    Car car = cf.getCar("luxury");
    car.drive();
    
  }
}

Output:  I am driving Honda

Here we are passing “luxury” string as a parameter and it gives an object of “Honda” Similarly, we can create an object of any car by just passing a string as a parameter. Here the user is only responsible to pass an only string as a parameter. It means, Which class object is initialized, is hidden from the user.

See More:
Abstract Factory Method: Creational design pattern
You must read these articles before going through this article....

Share your thoughts

Loading Facebook Comments ...
Loading Disqus Comments ...