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

What is Factory?

  • 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

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

  • 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.

--

--

--

A passionate in Tech https://www.linkedin.com/in/parathantl

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

New Electronic Instruments Part 2: components and wiring

How we used analytics to reduce Snowflake costs

BFF Dilemma — part 2

InsureDAO testnet

CodeRefactoring with Functional Interfaces

Strategies for Modernizing Legacy Enterprise Systems

Here are my Top 3 No-Code Website Builders.

Tools you Need as a Beginner Web Developer in 2022

Photo by <a href=”https://unsplash.com/@christinhumephoto?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Christin Hume</a> on <a href=”https://unsplash.com/s/photos/computer?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

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
Parathan Thiyagalingam

Parathan Thiyagalingam

A passionate in Tech https://www.linkedin.com/in/parathantl

More from Medium

Why do not use Math.random()

The S.O.L.I.D Principles

How To Write Clean Code

Rubber Duck Debugging