Factory Methods in Java - Types and Real-Time Usage
What is a Factory Method?
A Factory Method is a design pattern in object-oriented programming that provides an interface for
creating objects in a superclass, but allows subclasses to alter the type of objects that will be
created. Factory Methods are commonly used in real-world projects to promote loose coupling and
improve code maintainability.
Types of Factory Methods
1. Simple Factory
2. Factory Method Pattern
3. Abstract Factory
4. Singleton Factory
5. Parameterized Factory
Simple Factory
Definition: A method that returns an instance of different classes based on input parameters.
public class ShapeFactory {
public static Shape getShape(String shapeType) {
if (shapeType == null) return null;
if (shapeType.equalsIgnoreCase("CIRCLE")) return new Circle();
if (shapeType.equalsIgnoreCase("RECTANGLE")) return new Rectangle();
return null;
}
}
Factory Method Pattern
Definition: Provides an interface for creating objects but allows subclasses to decide which class to
instantiate.
public abstract class PaymentProcessor {
public abstract Payment getPaymentMethod();
}
Page 1
Factory Methods in Java - Types and Real-Time Usage
public class CreditCardProcessor extends PaymentProcessor {
public Payment getPaymentMethod() {
return new CreditCardPayment();
}
}
Abstract Factory
Definition: A factory of factories. It creates families of related objects without specifying their
concrete classes.
public interface UIComponentFactory {
Button createButton();
Checkbox createCheckbox();
}
public class WindowsFactory implements UIComponentFactory {
public Button createButton() { return new WindowsButton(); }
public Checkbox createCheckbox() { return new WindowsCheckbox(); }
}
Singleton Factory
Definition: A factory that ensures only one instance exists and provides a global access point to it.
public class DatabaseConnectionFactory {
private static DatabaseConnection instance;
private DatabaseConnectionFactory() {}
public static DatabaseConnection getInstance() {
if (instance == null) {
instance = new DatabaseConnection();
}
return instance;
}
}
Parameterized Factory
Definition: A factory method that takes parameters to customize the object creation.
Page 2
Factory Methods in Java - Types and Real-Time Usage
public class ReportFactory {
public static Report generateReport(String reportType) {
if (reportType.equalsIgnoreCase("PDF")) return new PDFReport();
if (reportType.equalsIgnoreCase("Excel")) return new ExcelReport();
return null;
}
}
Summary of Types of Factory Methods
1. Simple Factory: Centralize object creation logic. Example: LoggerFactory, DriverManager
2. Factory Method: Allow subclasses to decide the object to create. Example: Payment gateways,
shape creation
3. Abstract Factory: Create related objects without specifying concrete classes. Example: UI
components (Windows/Mac)
4. Singleton Factory: Ensure a single instance of the factory. Example: Database connection factory
5. Parameterized Factory: Pass parameters to customize object creation. Example: Report
generation (PDF/Excel)
Page 3