0% found this document useful (0 votes)
11 views

Factory Methods Types in Java

The document explains the Factory Method design pattern in Java, which provides an interface for creating objects while allowing subclasses to specify the object types. It outlines five types of Factory Methods: Simple Factory, Factory Method Pattern, Abstract Factory, Singleton Factory, and Parameterized Factory, each with definitions and examples. The summary highlights the purpose of each type, such as centralizing object creation logic and ensuring single instances.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Factory Methods Types in Java

The document explains the Factory Method design pattern in Java, which provides an interface for creating objects while allowing subclasses to specify the object types. It outlines five types of Factory Methods: Simple Factory, Factory Method Pattern, Abstract Factory, Singleton Factory, and Parameterized Factory, each with definitions and examples. The summary highlights the purpose of each type, such as centralizing object creation logic and ensuring single instances.

Uploaded by

suresh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Factory Methods in Java and Their Types

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 used to hide object creation logic and provide a common interface for

creating instances of different types.

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.

Example:

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;

Page 1
Factory Methods in Java and Their Types

Factory Method Pattern

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.

Example: Database connection factory.

Parameterized Factory

Definition: A factory method that takes parameters to customize the object creation.

Example: Report generation (PDF or Excel) based on input parameters.

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

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

5. Parameterized Factory: Pass parameters to customize object creation. Example: Report

generation (PDF/Excel)

Page 3

You might also like