Understanding the Factory Pattern: Simple Factory, Factory Method, and Abstract Factory with Real-world Examples
This article explains the three main factory design patterns—Simple Factory, Factory Method, and Abstract Factory—detailing their concepts, typical use cases such as document parsing and browser automation, and providing complete Java‑style code examples to illustrate each pattern's implementation and benefits.
Design patterns are essential skills for writing good code; the factory pattern is a creational pattern that hides object creation logic behind a common interface.
Simple Factory is suitable when the number of products is small; the article shows a document‑parsing scenario where a SimpleFactory creates Paragraph , Table , and Draw parsers, with full Java‑style source code.
public class DocxPaser {
//抽象产品:所有Parser共有的公共接口
public interface IPaser {
void process(string entity);
}
//具体产品:Paragraph Parser
static class Paragraph implements IPaser {
public void process(string entity) {
System.out.println("解析 Paragraph...");
}
}
//具体产品:Table Parser
static class Table implements IPaser {
public void process(string entity) {
System.out.println("解析 Table...");
}
}
//具体产品:Draw Parser
static class Draw implements IPaser {
public void process(string entity) {
System.out.println("解析 Draw...");
}
}
final class Const {
static final int ENTITY_PARAGRAPHP = 0;
static final int ENTITY_TABLE = 1;
static final int ENTITY_DRAW = 2;
}
//简单工厂:负责实现创建所有具体Parser实例的内部逻辑
static class ParserFactory {
public static IPaser creatParser(int kind) {
switch (kind) {
case Const.ENTITY_PARAGRAPHP:
return new Paragraph();
case Const.ENTITY_TABLE:
return new Table();
case Const.ENTITY_DRAW:
return new Draw();
}
return null;
}
}
// 简单使用示例
public static void main(String[] args) {
//entity 对应document.xml 此处略去具体获取过程
...
//解析paragraph
ParserFactory.creatParser(Const.ENTITY_PARAGRAPHP).process(entity);
//解析table
ParserFactory.creatParser(Const.ENTITY_TABLE).process(entity);
//解析draw
ParserFactory.creatParser(Const.ENTITY_DRAW).process(entity);
...
}
}Factory Method solves the open/closed principle by defining an abstract creator interface and letting subclasses instantiate specific products; the article demonstrates this with a browser‑automation example, defining an abstract Driver , concrete drivers ( ChromeDriver , FirefoxDriver , SafariDriver ), and corresponding factories that implement an AbstractFactory interface.
public class FactoryDemo {
//抽象对象:所有驱动对象的公共父类
abstract static class Driver {
abstract void process();
}
// 具体驱动对象:Chrome浏览器驱动对象
static class ChromeDriver extends Driver {
@Override
void process() {
System.out.println("ChromeDriver process"); // do something
}
}
// 具体驱动对象:Firefox浏览器驱动对象
static class FirefoxDriver extends Driver {
@Override
void process() {
System.out.println("FirefoxDriver process"); // do something
}
}
// 具体驱动对象:Safari浏览器驱动对象
static class SafariDriver extends Driver {
@Override
void process() {
System.out.println("SafariDriver process"); // do something
}
}
// 抽象工厂 所有工厂的公共接口
public interface AbstractFactory {
Driver create();
}
// 具体工厂:负责Chrome浏览器驱动对象的创建
static class ChromeDriverFactory implements AbstractFactory {
@Override
public Driver create() {
return new ChromeDriver();
}
}
// 具体工厂:负责Firefox浏览器驱动对象的创建
static class FirefoxDriverFactory implements AbstractFactory {
@Override
public Driver create() {
return new FirefoxDriver();
}
}
// 具体工厂:负责Safari浏览器驱动对象的创建
static class SafariDriverFactory implements AbstractFactory {
@Override
public Driver create() {
return new SafariDriver();
}
}
public static void main(String[] args) {
// 创建 Chrome 驱动对象
AbstractFactory chromeDriverFactory = new ChromeDriverFactory();
Driver chromeDriver = chromeDriverFactory.create();
chromeDriver.process();
// 创建 Firefox 驱动对象
AbstractFactory firefoxDriverFactory = new FirefoxDriverFactory();
Driver firefoxDriver = firefoxDriverFactory.create();
firefoxDriver.process();
// ...
}
}Abstract Factory further generalizes the concept to create families of related objects; a cross‑platform membership scenario shows an abstract Vip product, concrete implementations for iOS, Android, and PC, and platform‑specific factories that produce the appropriate Vip objects.
//抽象产品
public interface Vip {}
//具体产品
public class NormalVip implements Vip {}
public class MonthlyVip implements Vip {}
public class IOSNormalVip extends NormalVip {}
public class AndroidNormalVip extends NormalVip {}
public class PCNormalVip extends NormalVip {}
public class IOSMonthlyVip extends MonthlyVip {}
public class AndroidMonthlyVip extends MonthlyVip {}
public class PCMonthlyVip extends MonthlyVip {}
//抽象工厂
public interface AbstractVipFactory {
Vip createNormalVip();
Vip createMonthlyVip();
}
//具体工厂
public class IOSVipFactory implements AbstractVipFactory {
@Override
public Vip createNormalVip() { return new IOSNormalVip(); }
@Override
public Vip createMonthlyVip() { return new IOSMonthlyVip(); }
}
//调用示例
public class Client {
public static void main(String[] args) {
IOSVipFactory iosVipFactory = new IOSVipFactory();
Vip iosNormalVip = iosVipFactory.createNormalVip();
}
}The article concludes that simple factory fits scenarios with few product types, factory method is appropriate for complex product hierarchies, and abstract factory is best when multiple families of products need to be created together.
Architecture Digest
Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.
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.