Factory Method Pattern: Definition, Structure, Implementation, Advantages, and Use Cases
This article explains the Factory Method design pattern, covering its definition, UML diagram, key participants, the problems it solves compared to the Simple Factory, step‑by‑step Java implementation with code examples, advantages, disadvantages, application scenarios, and differences from the Simple Factory pattern.
The Factory Method pattern (also known as Factory pattern) abstracts and upgrades the Simple Factory pattern, providing higher extensibility and better adherence to the Open/Closed principle.
Definition: A factory superclass defines a common interface for creating objects, while subclasses instantiate concrete products.
Key participants (4 roles): Product (abstract product), ConcreteProduct (concrete product implementing Product), Factory (abstract factory returning a Product), ConcreteFactory (concrete factory returning a ConcreteProduct).
Problem solved: Unlike Simple Factory, adding a new product does not require modifying the existing factory class, thus avoiding violation of the Open/Closed principle and reducing coupling.
Implementation in Java (code snippets):
package com.mikechen.java.design.factory.simple;
public abstract class Phone {
public abstract void start();
}
public class HuaWeiPhone extends Phone {
@Override
public void start() {
System.out.println("华为手机启动");
}
}
public class XiaoMiPhone extends Phone {
@Override
public void start() {
System.out.println("小米手机启动");
}
}
public class IPhone extends Phone {
@Override
public void start() {
System.out.println("苹果手机启动");
}
}
public abstract class PhoneFactory {
public abstract T createPhone(Class clz);
}
public class HQBPhoneFactor extends PhoneFactory {
@Override
public T createPhone(Class clz) {
Phone phone = null;
String classname = clz.getName();
try {
phone = (Phone) Class.forName(classname).newInstance();
} catch (Exception e) {
e.printStackTrace();
}
return (T) phone;
}
}
public class Client {
public static void main(String[] args) {
PhoneFactory phoneFactory = new HQBPhoneFactor();
HuaWeiPhone huawei = phoneFactory.createPhone(HuaWeiPhone.class);
huawei.start();
XiaoMiPhone xiaomi = phoneFactory.createPhone(XiaoMiPhone.class);
xiaomi.start();
IPhone iphone = phoneFactory.createPhone(IPhone.class);
iphone.start();
}
}
Advantages: Increases flexibility (new products only need a new factory), provides a typical decoupling framework where high‑level modules depend on abstractions only.
Disadvantages: Increases system complexity because each new product adds a corresponding concrete factory, and a concrete factory can create only one product type.
Application scenarios: When a class does not know which concrete product it needs, or when a class wants its subclasses to decide which object to create.
Difference from Simple Factory: Simple Factory embeds creation logic within a single factory class, violating the Open/Closed principle; Factory Method delegates object creation to subclasses, preserving the principle.
Conclusion: The Factory Method pattern embodies interface‑oriented programming, offering better extensibility and polymorphism than Simple Factory while retaining encapsulation benefits.
Mike Chen's Internet Architecture
Over ten years of BAT architecture experience, shared generously!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.