The Factory Pattern provides an interface for creating objects, but lets subclasses decide which class to instantiate. Factory Methods let a class defer instantiation to subclasses.
The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Factory patterns encapsulate object creation, making code more flexible and less dependent on specific classes.
- Creator: Abstract class that declares the factory method
- ConcreteCreator: Implements the factory method to create specific products
- Product: The interface for objects the factory method creates
- ConcreteProduct: The actual objects created by the factory method
- AbstractFactory: Interface declaring methods for creating abstract products
- ConcreteFactory: Implements operations to create concrete product objects
- AbstractProduct: Interface for a type of product
- ConcreteProduct: Product objects created by the corresponding concrete factory
- Client: Uses only interfaces declared by AbstractFactory and AbstractProduct
Demonstrates the Factory Method Pattern with a pizza store franchise where each regional store creates pizzas in its own way.
See the FactoryPatternPizzaStore README for details.
Shows the Abstract Factory Pattern where pizza stores use ingredient factories to create families of related ingredients (dough, sauce, cheese, etc.).
See the AbstractFactoryPatternPizzaStore README for details.
From the parent directory of FactoryPattern:
javac -d out FactoryPattern/FactoryPatternPizzaStore/*.javaOr for the Abstract Factory example:
javac -d out FactoryPattern/AbstractFactoryPatternPizzaStore/*.javajava -cp out FactoryPattern.FactoryPatternPizzaStore.PizzaTestDriveOr for the Abstract Factory example:
java -cp out FactoryPattern.AbstractFactoryPatternPizzaStore.PizzaTestDrive- Decoupling: Decouples client code from concrete classes
- Flexibility: Easy to introduce new types of products
- Single Responsibility: Object creation code is in one place
- Open/Closed Principle: Can introduce new product types without changing existing code
- Dependency Inversion: Depend on abstractions, not concrete classes
- Uses inheritance (subclass decides which class to instantiate)
- Creates one product
- Good when you have one product hierarchy
- Uses composition (object delegates to factory object)
- Creates families of related products
- Good when you need to create families of products that work together
Use the Factory Method Pattern when:
- A class can't anticipate the type of objects it needs to create
- A class wants its subclasses to specify the objects it creates
- Classes delegate responsibility to helper subclasses
Use the Abstract Factory Pattern when:
- A system should be independent of how its products are created
- A system should be configured with one of multiple families of products
- A family of related products is designed to be used together
- You want to provide a library of products and reveal only their interfaces