Factory Pattern: Simple Factory, Factory Method, and Abstract Factory – Concepts, Code, UML, Pros & Cons
The article explains the Factory design pattern family—Simple Factory, Factory Method, and Abstract Factory—showing how each separates object creation from use, presents Java code and UML examples, and compares their advantages such as decoupling and Open/Closed compliance against drawbacks like increased complexity and class proliferation.
This article focuses on the Factory design pattern, covering Simple Factory, Factory Method, and Abstract Factory. It explains why design patterns improve code reusability, maintainability, and extensibility.
Core Idea : Separate object creation from usage by letting a factory create objects, allowing clients to work with interfaces only.
Simple Factory
Structure: Abstract product, concrete products, and a factory class with a static method that returns a product based on a parameter.
// step1: create abstract product class
public abstract class Shirt {
public abstract void Show();
}
// step2: concrete product classes
public class WomenShirt extends Shirt {
@Override
public void Show() { System.out.println("展示女款衬衫"); }
}
public class MenShirt extends Shirt {
@Override
public void Show() { System.out.println("展示男款衬衫"); }
}
// step3: factory class
public class Factory {
public static Shirt Exhibit(String name) {
switch(name) {
case "女款衬衫": return new WomenShirt();
case "男款衬衫": return new MenShirt();
default: return null;
}
}
}
// step4: client usage
public class SimpleFactoryPattern {
public static void main(String[] args) {
Factory f = new Factory();
try { f.Exhibit("女款衬衫").Show(); } catch (NullPointerException e) { System.out.println("没有找到商品"); }
try { f.Exhibit("男款衬衫").Show(); } catch (NullPointerException e) { System.out.println("没有找到商品"); }
}
}Pros: decouples creation, easy maintenance, follows Open/Closed principle. Cons: factory class can become complex, static methods cannot be overridden.
Factory Method
Structure: Abstract product, concrete products, abstract factory, concrete factories. Each concrete factory creates one type of product.
// abstract factory
public abstract class Factory {
public abstract Shirt Exhibit();
}
// concrete factories
public class WomenShirtFactory extends Factory {
@Override
public Shirt Exhibit() { return new WomenShirt(); }
}
public class MenShirtFactory extends Factory {
@Override
public Shirt Exhibit() { return new MenShirt(); }
}
// client
public class FactoryPattern {
public static void main(String[] args) {
Factory f1 = new WomenShirtFactory();
f1.Exhibit().Show();
Factory f2 = new MenShirtFactory();
f2.Exhibit().Show();
}
}Pros: adheres to Open/Closed principle, each factory handles a single product, supports inheritance. Cons: each factory creates only one product, increasing class count.
Abstract Factory
Structure: Abstract factory defines methods for creating families of related products (e.g., shirts and trousers). Concrete factories produce concrete products for a specific product family.
// abstract factory for product families
public abstract class Factory {
public abstract Clothing ExhibitShirt();
public abstract Clothing ExhibitTrousers();
}
// concrete factories for different shops
public class TBFactory extends Factory {
@Override public Clothing ExhibitShirt() { return new TBShirt(); }
@Override public Clothing ExhibitTrousers() { return new TBTrousers(); }
}
public class TTFactory extends Factory {
@Override public Clothing ExhibitShirt() { return new TTShirt(); }
@Override public Clothing ExhibitTrousers() { return new TTTrousers(); }
}
// client usage
public class AbstractFactoryPattern {
public static void main(String[] args) {
Factory tb = new TBFactory();
tb.ExhibitShirt().Show();
tb.ExhibitTrousers().Show();
Factory tt = new TTFactory();
tt.ExhibitShirt().Show();
tt.ExhibitTrousers().Show();
}
}Pros: reduces coupling, supports product families, complies with Open/Closed and Single Responsibility principles. Cons: adding new product types requires changes to the abstract factory and all concrete factories.
Summary
Simple Factory is easy but violates Open/Closed; Factory Method improves extensibility but each factory handles one product; Abstract Factory handles families of products, offering the highest flexibility at the cost of complexity.
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.
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.
