Factory Pattern and Abstract Factory Pattern are important Creational Design Patterns in Java. These patterns are commonly asked in interviews to test your understanding of object creation, loose coupling, runtime flexibility, and families of related objects.
Q1. You are developing a software system that needs to generate multiple types of reports (e.g., PDF, Excel, HTML). The specific report type required is only known at runtime, and each report type has a distinct creation process. Which of the following reasons justify using the Factory pattern in this scenario?
Options:
- A) It allows for dynamic creation of report types at runtime.
- B) It minimizes dependencies by hiding report creation details from the client.
- C) It ensures all reports follow a single instantiation process, even if they differ significantly.
- D) It allows switching report types without modifying client code.
Answer:
A) It allows for dynamic creation of report types at runtime; B) It minimizes dependencies by hiding report creation details from the client; D) It allows switching report types without modifying client code.
Explanation:
The Factory pattern is appropriate here because it:
- A) Correct. Allows dynamic creation of different report types based on runtime information.
- B) Correct. Encapsulates creation details so that the client doesn’t need to know the specifics of each report’s instantiation.
- C) Option C is incorrect, as the Factory pattern allows each report type to have its own instantiation process.
- D) Correct. Allows switching report types without altering client code, fostering flexibility and reducing maintenance.
Q2. In the Factory pattern, which of the following best describes the purpose of a Factory class?
Options:
- A) It is responsible for creating objects of subclasses based on conditions without exposing the exact subclass instantiation details.
- B) It stores all instances of subclasses created in the application for reference.
- C) It manages the lifecycle of objects, including cleanup and memory deallocation.
- D) It provides a global point of access to all instances created in the application.
Answer:
A) It is responsible for creating objects of subclasses based on conditions without exposing the exact subclass instantiation details.
Explanation:
In the Factory pattern, the Factory class abstracts the object creation process, allowing for flexible instantiation of different subclasses without exposing specific subclass details.
- B, C, and D refer to other patterns or concepts, such as object pooling or singleton access, which are unrelated to the Factory pattern.
Q3. Which of the following best describes the role of an Abstract Factory in the Abstract Factory Pattern?
Options:
- A) It defines a factory interface for creating a single product.
- B) It allows for the creation of families of related or dependent objects without specifying their concrete classes.
- C) It implements a single factory for all product types in the application.
- D) It uses prototyping to generate objects based on a predefined model.
Answer:
B) It allows for the creation of families of related or dependent objects without specifying their concrete classes.
Explanation:
- A) Incorrect. An Abstract Factory produces families of products, not just a single product.
- B) Correct. This is the defining characteristic of the Abstract Factory Pattern.
- C) Incorrect. The Abstract Factory typically defines multiple interfaces for different families of products.
- D) Incorrect. The Prototype Pattern, not Abstract Factory, is used for cloning objects.
Q4. You are implementing a travel booking system where each booking type (flights, hotels) has varying methods. You need a flexible way to switch between booking providers for each type. Which pattern should you use?
Options:
- A) Abstract Factory Pattern
- B) Decorator Pattern
- C) Singleton Pattern
- D) Chain of Responsibility Pattern
Answer:
A) Abstract Factory Pattern
Explanation:
- A) Abstract Factory Pattern: Correct. Abstract Factory allows creating related objects for each provider.
- B) Decorator Pattern: Incorrect. Decorator adds behaviour without managing object creation.
- C) Singleton Pattern: Incorrect. Singleton limits instances but isn’t ideal for managing providers.
- D) Chain of Responsibility Pattern: Incorrect. Chain handles sequential requests, not flexible creation.
Why These Patterns Matter in Java Interviews
Interviewers ask Factory and Abstract Factory questions to test:
- Runtime object creation
- Loose coupling
- Encapsulation
- Dependency reduction
- Product families
- Scalability in large systems
- Design thinking in real projects
Common Real-World Examples
Factory Pattern:
- Report generators
- Notification systems
- Payment gateway creation
- Logger creation
Abstract Factory Pattern:
- UI themes (Windows / Mac)
- Database drivers
- Travel booking providers
- Cloud service families

