Understanding Factory Patterns: Simple Factory, Factory Method, and Abstract Factory with Code Samples
The article explains the Factory design pattern’s three main variants—Simple Factory for limited product types, Factory Method for extensible hierarchies, and Abstract Factory for creating families of related objects—illustrated with Java code examples for document parsing, automated testing, and cross‑platform membership systems.
Design patterns are essential skills for writing good code. This article introduces the Factory Pattern, a creational pattern that provides the best way to create objects without exposing the creation logic to the client.
The Factory Pattern includes three main variants:
Simple Factory Pattern
Factory Method Pattern
Abstract Factory Pattern
1. Simple Factory in Document Parsing
When the number of product types is small, a single factory class can create the required objects. In a Word2007 document parsing scenario, different parsers (Paragraph, Table, Draw) are created by a simple factory, keeping the parsing logic decoupled from the client.
public class DocxPaser {
// Abstract product: common interface for all parsers
public interface IPaser {
void process(String entity);
}
// Concrete product: Paragraph Parser
static class Paragraph implements IPaser {
public void process(String entity) {
System.out.println("解析 Paragraph...");
}
}
// Concrete product: Table Parser
static class Table implements IPaser {
public void process(String entity) {
System.out.println("解析 Table...");
}
}
// Concrete product: 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;
}
// Simple factory: creates parser instances
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;
}
}
// Simple usage example
public static void main(String[] args) {
// entity corresponds to document.xml (omitted)
// Parse paragraph
ParserFactory.creatParser(Const.ENTITY_PARAGRAPHP).process(entity);
// Parse table
ParserFactory.creatParser(Const.ENTITY_TABLE).process(entity);
// Parse draw
ParserFactory.creatParser(Const.ENTITY_DRAW).process(entity);
}
}2. Factory Method in Automated Testing
When many product types need to be created and the set changes frequently, the Simple Factory violates the Open‑Closed Principle. The Factory Method defines an interface for creating objects, and each concrete factory implements this interface, allowing new product types without modifying existing code.
Example: different browser drivers (Chrome, Firefox, Safari) for automated UI testing.
public class FactoryDemo {
// Abstract product: driver base class
abstract static class Driver {
abstract void process();
}
// Concrete drivers
static class ChromeDriver extends Driver {
@Override void process() { System.out.println("ChromeDriver process"); }
}
static class FirefoxDriver extends Driver {
@Override void process() { System.out.println("FirefoxDriver process"); }
}
static class SafariDriver extends Driver {
@Override void process() { System.out.println("SafariDriver process"); }
}
// Abstract factory interface
public interface AbstractFactory { Driver create(); }
// Concrete factories
static class ChromeDriverFactory implements AbstractFactory {
@Override public Driver create() { return new ChromeDriver(); }
}
static class FirefoxDriverFactory implements AbstractFactory {
@Override public Driver create() { return new FirefoxDriver(); }
}
static class SafariDriverFactory implements AbstractFactory {
@Override public Driver create() { return new SafariDriver(); }
}
public static void main(String[] args) {
AbstractFactory chromeFactory = new ChromeDriverFactory();
Driver chrome = chromeFactory.create();
chrome.process();
AbstractFactory firefoxFactory = new FirefoxDriverFactory();
Driver firefox = firefoxFactory.create();
firefox.process();
}
}3. Abstract Factory in Cross‑Platform Scenarios
The Abstract Factory extends the Factory Method by allowing a factory to create families of related objects. In a cross‑platform membership system (iOS, Android, PC), each platform may have different implementations for normal and monthly VIP members. Using an abstract factory, the client can obtain the appropriate concrete product without knowing the platform details.
// Abstract product
public interface Vip {}
// Concrete products
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 {}
// Abstract factory
public interface AbstractVipFactory {
Vip createNormalVip();
Vip createMonthlyVip();
}
// Concrete factories
public class IOSVipFactory implements AbstractVipFactory {
@Override public Vip createNormalVip() { return new IOSNormalVip(); }
@Override public Vip createMonthlyVip() { return new IOSMonthlyVip(); }
}
// Usage example
public class Client {
public static void main(String[] args) {
IOSVipFactory iosFactory = new IOSVipFactory();
Vip iosNormal = iosFactory.createNormalVip();
}
}Conclusion
Through three practical cases—document parsing, automated testing, and cross‑platform membership—the article demonstrates when to choose Simple Factory, Factory Method, or Abstract Factory. Simple Factory suits complex instantiation with few product types, Factory Method fits complex product hierarchies, and Abstract Factory is ideal for many product families.
Baidu Geek Talk
Follow us to discover more Baidu tech insights.
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.