Design Patterns — Zero to Hero — Factory Pattern (Simple Factory and Factory Method)

What is Factory?

A place where things are created/produced. In factories things are created/produces according to the requirements/orders. Similarly, in the Software development Factory Pattern is implemented to create and execute objects according to the parameters passed.

  • Simple Factory: allows interfaces for creating objects without exposing the object creation logic to the client
  • Factory Method: allows interfaces for creating objects, but allow sub-classes to determine which class to instantiate
  • Abstract Factory: an abstract factory provides a way to create families of objects for a group of factories without specifying its concrete class. (We will explore this in our next article.)
Class() obj = new Class();

Simple Factory Pattern

The Simple Factory Pattern describes a way of instantiating class using a method with a large conditional that based on method parameters to choose which product class to instantiate and then return.

See the following interface and the class implementations.

  • IMobile interface
public interface IMobile {
void sendMessage();
}

Samsung class implementation

public class Samsung implements IMobile{    @Override
public void sendMessage() {
System.out.println("Samsung Selected");
}
}

iPhone Class implementation

public class iPhone implements IMobile {@Override
public void sendMessage() {
System.out.println("iPhone Selected ");
}
}
public class App {     public static void main(String[] args) {         IMobile mob1 = new Samsung();    
mob1.sendMessage();
IMobile mob2 = new iPhone();
mob2.sendMessage();
}}
public class MobileFactory {       IMobile mobile = null;       //getType method will return IMobileobject creation according              to the parameter passsed       IMobile getType(String str) {             if(str.equals("Samsung")) {
mobile = new Samsung();
}else if(str.equals("iPhone")) {
mobile = new iPhone();
}
return mobile;
}
}
public class App {
public static void main(String[] args) {
MobileFactory mobfactory = new MobileFactory(); mobfactory.getType("Samsung").sendMessage();;
}
}

Factory Method Pattern

Factory Method Pattern says that just define an interface or an abstract class and let the subclasses decide which class to instantiate.

  • Create an abstract/interface for Brands
  • Brands will be implemented or extended by the concrete classes. Eg: Canon, Sony
  • Another interface for Equipment that will be extended/implemented by the concrete classes. Eg: CanonDSLR, CanonScanner, SonyTV, and SonyDSLR
  • getDetails() method in the subclasses of Brands will decide which Equipment class will be selected according to the parameter value of Equipment (Eg: Camera, TV, Scanner) passed.

Brands abstract class

public abstract Brands{       public Equipment orderEquipment(String equipment){
Equipment equip;
equip = getDetail(equipment);
return equip; } protected abstract Equipment getDetail(String brandName)}

Canon class which extends Brands class

public Canon extends Brands{     protected Equipment getDetail(String equipment){
Equipment equip = null;
if(equipment.equals("DSLR")){
equip = new CanonDSLR();
}else if(equipment.equals("Scanner")){
equip = new CannonScanner();
}else if(equipment.equals("Printer")){
equip = new CanonPrinter()
}
return equip;
}
}

Equipment interface (This can be implemented as an abstract class if you want to have any variables or method implementations)

public interface Equipment{        void printBrand();}

CanonDSLR class

public class CanonDSLR implements Equipment{    @Override
public void printBrand() {
System.out.println("Canon DSLR Selected");
}
}
public class App {    public static void main(String[] args) {         Brands canon1 = new Canon();         Equipment equip1 = canon1.getDetail("DSLR");         equip1.printBrand();    }}

Usage of the Factory Method Pattern

  • When a class doesn’t know which sub-classes will be required to create
  • When a class wants its subclasses to specify its objects to be created
  • When the parent classes choose the creation of objects to its sub-classes.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store