FACTORY PATTERN

Factory design pattern is used when we have a super class with multiple sub-classes and based on input, we need to return one of the sub-classes. This pattern takes out the responsibility of instantiation of a class from client program to the factory class.

We can apply Singleton pattern on Factory class or make the factory method static. Super class in factory pattern can be an interface or a normal java class.

Benefits: 

  • Factory pattern provides approach to code for interface rather than implementation.
  • Factory pattern removes the instantiation of actual implementation classes from client code, making it more robust, less coupled and easy to extend. For example, we can easily change PC class implementation because client program is unaware of this.
  • Factory pattern provides abstraction between implementation and client classes through inheritance .

Example:

ILaptop.java interface

package com.myjavablog.factory;

public interface ILaptop {

public void processor();

public void cost();

public void ramSize();
}

Dell.java

package com.myjavablog.factory;

public class Dell implements ILaptop {

@Override
public void processor() {
System.out.println("Processor available in AMD, I3, I5, I7");
}

@Override
public void cost() {
System.out.println("Cost starts from 18k");

}

@Override
public void ramSize() {
System.out.println("RAM size starts from 1 GB");
}
}

Samsung.java

package com.myjavablog.factory;

public class Samsung implements ILaptop {

@Override
public void processor() {
System.out.println("Processor available in I3, I5, I7");
}

@Override
public void cost() {
System.out.println("Cost starts from 20k");

}

@Override
public void ramSize() {
System.out.println("RAM size starts from 2 GB");
}
}

Lenovo.java

package com.myjavablog.factory;

public class Lenovo implements ILaptop {

@Override
public void processor() {
System.out.println("Processor available in I3, I5");
}

@Override
public void cost() {
System.out.println("Cost starts from 25k");

}

@Override
public void ramSize() {
System.out.println("RAM size starts from 4 GB");
}
}

Now create a factory class LaptopFactory.java

package com.myjavablog.factory;

public class LaptopFactory {

public ILaptop createLaptop(String type) {

ILaptop laptop = null;

if("sam".equalsIgnoreCase(type)){
laptop = new Samsung();
System.out.println("Samsung laptop created");
}else if("len".equalsIgnoreCase(type)){
laptop = new Lenovo();
System.out.println("Lenovo laptop created");
}if("dell".equalsIgnoreCase(type)){
laptop = new Dell();
System.out.println("Dell laptop created");
}
return laptop;
}
}

Now create a main class to test Factory TestFactory.java

package com.myjavablog.factory;

public class TestFactory {

public static void main(String[] args) {

LaptopFactory factory = new LaptopFactory();

Lenovo len = (Lenovo) factory.createLaptop("len");
len.processor();

Dell dell = (Dell) factory.createLaptop("dell");
dell.cost();

}
}

Output:

Lenovo laptop created
Processor available in I3, I5
Dell laptop created
Cost starts from 18k

Usage in JDK:

  • java.util.Calendar, ResourceBundle and NumberFormat getInstance () methods uses Factory pattern.
  • valueOf () method in wrapper classes like Boolean, Integer etc .

 

Leave a Comment

Bitnami