Design Pattern | Factory Method
What Is the Factory Method Pattern?
- The English word factory means a place that produces things. In a factory, products are made to match specifications, and when requirements change slightly, slightly different products are also produced.
- The Factory Method pattern creates an instance that matches the desired specification.
- The Factory Method pattern defines the way to create instances in the superclass, while concrete creation processing is performed by subclasses.
- The Factory Method pattern is a factory for creating instances structured with the Template Method pattern.
- In GoF design patterns, it is classified as a creational design pattern.
Factory Method Pattern Example Program
This program creates ID cards in an ID card factory.
Class Diagram

1. Product Class
This is the base class for objects created by the Factory.
Product.java
package com.devkuma.designpattern.creational.factorymethod.framwork;
public abstract class Product {
public abstract void use();
}
2. Factory Class
This is the base class of the Factory. It creates instances.
Factory.java
package com.devkuma.designpattern.creational.factorymethod.framwork;
public abstract class Factory {
public final Product create(String owner) {
Product product = createProduct(owner);
registerProduct(product);
return product;
}
protected abstract Product createProduct(String owner);
protected abstract void registerProduct(Product product);
}
3. IDCard Class
This is a concrete class that implements the methods defined by the Product class.
IDCard.java
package com.devkuma.designpattern.creational.factorymethod.idcard;
import com.devkuma.designpattern.creational.factorymethod.framwork.Product;
public class IdCard extends Product {
private String owner;
IdCard(String owner) {
System.out.println(owner + "의 카드를 만듭니다.");
this.owner = owner;
}
public void use() {
System.out.println(owner + "의 카드를 사용합니다.");
}
public String getOwner() {
return owner;
}
}
4. IDCardFactory Class
This is a concrete class that implements the methods defined by the Factory class.
IDCardFactory.java
package com.devkuma.designpattern.creational.factorymethod.idcard;
import com.devkuma.designpattern.creational.factorymethod.framwork.Factory;
import com.devkuma.designpattern.creational.factorymethod.framwork.Product;
import java.util.ArrayList;
public class IdCardFactory extends Factory {
private ArrayList<String> owners = new ArrayList();
protected Product createProduct(String owner) {
return new IdCard(owner);
}
protected void registerProduct(Product product) {
IdCard icCard = (IdCard) product;
String owner = icCard.getOwner();
owners.add(owner);
}
public ArrayList<String> getOwners() {
return owners;
}
}
5. Main Class
This is the main class that executes the main processing.
Main.java
package com.devkuma.designpattern.creational.factorymethod;
import com.devkuma.designpattern.creational.factorymethod.framwork.Factory;
import com.devkuma.designpattern.creational.factorymethod.framwork.Product;
import com.devkuma.designpattern.creational.factorymethod.idcard.IdCardFactory;
public class Main {
public static void main(String[] args) {
Factory factory = new IdCardFactory();
Product card1 = factory.create("devkuma");
Product card2 = factory.create("kimkc");
Product card3 = factory.create("yunho");
card1.use();
card2.use();
card3.use();
}
}
6. Execution Result
devkuma의 카드를 만듭니다.
kimkc의 카드를 만듭니다.
yunho의 카드를 만듭니다.
devkuma의 카드를 사용합니다.
kimkc의 카드를 사용합니다.
yunho의 카드를 사용합니다.
Advantages of the Factory Method Pattern
Factory/Product exist in the framework package, while IDCardFactory/IDCard exist in the idcard package.
The framework package does not import the idcard package. In other words, the framework package does not depend on the idcard package.
When creating completely different products and factories, there is no need to modify the contents of the framework package.