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

Factory Methods Styled in Java

The document explains Factory Methods in Java, a design pattern that allows subclasses to determine the type of objects created while promoting loose coupling and maintainability. 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 emphasizes the practical applications of these methods in real-world scenarios such as payment processing, UI components, and report generation.

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Factory Methods Styled in Java

The document explains Factory Methods in Java, a design pattern that allows subclasses to determine the type of objects created while promoting loose coupling and maintainability. 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 emphasizes the practical applications of these methods in real-world scenarios such as payment processing, UI components, and report generation.

Uploaded by

suresh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

You might also like