Mastering Factory Patterns: Simple, Factory Method & Abstract Factory Explained
This article reviews creational design patterns—Simple Factory, Factory Method, and Abstract Factory—explaining why factories are needed, outlining their roles, comparing each pattern, and providing Java code demos with practical usage scenarios and best‑practice recommendations.
Preface
The core creational patterns—Simple Factory, Factory Method, and Abstract Factory—are revisited and compared.
Why Use Factories
Creating objects directly with new() couples object creation logic with business logic. Factory patterns decouple non‑core creation logic from the main flow, allowing clients to obtain objects via factory methods.
Roles in Factory Patterns
Abstract Product
Concrete Product
Abstract Factory (present in Factory Method and Abstract Factory)
Concrete Factory
Context (client that uses the factory)
Basic Understanding of Factories
A factory method receives parameters and returns a product instance; the returned type is the abstract product, enabling the method to create different concrete products based on conditions.
Simple Classification of Factory Patterns
1. Simple Factory
Only one factory class without an abstract layer; the factory decides which concrete product to create based on parameters. It does not follow the Open/Closed Principle because adding a new product requires modifying the factory.
2. Factory Method
Both product and factory have abstract layers; each concrete factory creates a single product type, adhering to the Open/Closed Principle—adding a product only requires a new concrete factory.
3. Abstract Factory
Both product and factory have abstract layers; a factory can create multiple product families (e.g., CPU, Mainboard). It solves the problem of switching entire product families without modifying existing factories.
Simple Factory Demo
package com.taobao.migao.pattern.factory.simplefactory;
public interface Cpu {
void calculate();
}
public class ACpu implements Cpu {
@Override
public void calculate() {
System.out.println("this is A cpu");
}
}
public class BCpu implements Cpu {
@Override
public void calculate() {
System.out.println("this is B cpu");
}
}
public class CpuFactory {
public static Cpu createCpu(Class classType) {
if (classType.getName().equals(ACpu.class.getName())) {
return new ACpu();
} else if (classType.getName().equals(BCpu.class.getName())) {
return new BCpu();
}
return null;
}
}
public class SimpleFactoryTest {
public static void main(String[] args) {
Cpu cpu = CpuFactory.createCpu(BCpu.class);
cpu.calculate();
}
}Factory Method Demo
package com.taobao.migao.pattern.factory.factorymethod;
public interface Cpu {
void calculate();
}
public class ACpu implements Cpu {
@Override
public void calculate() {
System.out.println("this is A cpu");
}
}
public class BCpu implements Cpu {
@Override
public void calculate() {
System.out.println("this is B cpu");
}
}
public interface CpuFactory {
Cpu createCpu();
}
public class ACpuFactory implements CpuFactory {
@Override
public Cpu createCpu() {
return new ACpu();
}
}
public class BCpuFactory implements CpuFactory {
@Override
public Cpu createCpu() {
return new BCpu();
}
}
public class FactoryMethodTest {
public static void main(String[] args) {
CpuFactory factory = new ACpuFactory();
Cpu cpu = factory.createCpu();
cpu.calculate();
}
}Differences Between Simple Factory and Factory Method
Simple Factory: client passes a type parameter; the factory contains the decision logic.
Factory Method: each concrete factory contains pure creation logic; the client decides which factory to use.
Adding a new product requires modifying the simple factory, while the factory method only needs a new concrete factory.
Differences Between Factory Method and Abstract Factory
Factory Method focuses on a single product hierarchy (e.g., CPU).
Abstract Factory handles multiple product hierarchies (e.g., CPU, Mainboard, GPU) within a product family.
Abstract Factory Demo
package com.taobao.migao.pattern.factory.abstractfactory;
public interface Cpu {
void calculate();
}
public interface Mainboard {
void installCpu();
}
public interface AbstractFactory {
Cpu createCpu();
Mainboard createMainboard();
}
public class ACpu implements Cpu {
@Override
public void calculate() {
System.out.println("this is A cpu");
}
}
public class BCpu implements Cpu {
@Override
public void calculate() {
System.out.println("this is B cpu");
}
}
public class AMainboard implements Mainboard {
@Override
public void installCpu() {
System.out.println("this is A mainboard");
}
}
public class BMainboard implements Mainboard {
@Override
public void installCpu() {
System.out.println("this is B mainboard");
}
}
public class AFactory implements AbstractFactory {
@Override
public Cpu createCpu() {
return new ACpu();
}
@Override
public Mainboard createMainboard() {
return new AMainboard();
}
}
public class BFactory implements AbstractFactory {
@Override
public Cpu createCpu() {
return new BCpu();
}
@Override
public Mainboard createMainboard() {
return new BMainboard();
}
}
public class AbstractFactoryTest {
public static void main(String[] args) {
AbstractFactory factory = new AFactory();
Cpu cpu = factory.createCpu();
Mainboard mainboard = factory.createMainboard();
cpu.calculate();
mainboard.installCpu();
}
}When to Use Each Factory Pattern
A system should not depend on how product instances are created or combined. Use Simple Factory for straightforward creation when only one product family exists. Choose Factory Method when each product family needs its own factory to satisfy the Open/Closed Principle. Adopt Abstract Factory when multiple product families (e.g., CPU, Mainboard, GPU) must be created together as a cohesive family.
Pros and Cons of Abstract Factory
Advantages
Separates interface from implementation, keeping client code decoupled.
Switching product families becomes trivial—swap the concrete factory.
Disadvantages
Adding a new product type requires changes to the abstract factory and all concrete factories.
Best Practices
Prefer Abstract Factory when you have multiple related products that are used together; otherwise, Simple Factory or Factory Method may be sufficient.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
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.
