Factory Methods Types in Java
Factory Methods Types in Java
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 used to hide object creation logic and provide a common interface for
1. Simple Factory
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.
Example:
return null;
Page 1
Factory Methods in Java and Their Types
Definition: Provides an interface for creating objects but allows subclasses to decide which class to
instantiate.
Example: Payment processors create different payment methods (Credit Card, UPI, etc.).
Abstract Factory
Definition: A factory of factories. It creates families of related objects without specifying their
concrete classes.
Example: UI components like buttons and checkboxes for different operating systems (Windows,
Mac, etc.).
Singleton Factory
Definition: A factory that ensures only one instance exists and provides a global access point to it.
Parameterized Factory
Definition: A factory method that takes parameters to customize the object creation.
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
Page 2
Factory Methods in Java and Their Types
components (Windows/Mac)
4. Singleton Factory: Ensure a single instance of the factory. Example: Database connection factory
generation (PDF/Excel)
Page 3