Skip to content

Latest commit

 

History

History
105 lines (67 loc) · 3.53 KB

File metadata and controls

105 lines (67 loc) · 3.53 KB

Factory Pattern

Overview

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.

Pattern Structure

Factory patterns encapsulate object creation, making code more flexible and less dependent on specific classes.

Key Concepts

Factory Method Pattern

  • 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

Abstract Factory Pattern

  • 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

Examples

FactoryPatternPizzaStore

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.

AbstractFactoryPatternPizzaStore

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.

How to Compile

From the parent directory of FactoryPattern:

javac -d out FactoryPattern/FactoryPatternPizzaStore/*.java

Or for the Abstract Factory example:

javac -d out FactoryPattern/AbstractFactoryPatternPizzaStore/*.java

How to Run

java -cp out FactoryPattern.FactoryPatternPizzaStore.PizzaTestDrive

Or for the Abstract Factory example:

java -cp out FactoryPattern.AbstractFactoryPatternPizzaStore.PizzaTestDrive

Benefits of Factory Patterns

  • 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

Factory Method vs Abstract Factory

Factory Method

  • Uses inheritance (subclass decides which class to instantiate)
  • Creates one product
  • Good when you have one product hierarchy

Abstract Factory

  • Uses composition (object delegates to factory object)
  • Creates families of related products
  • Good when you need to create families of products that work together

When to Use

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