What is the main purpose of the Factory Method pattern?
The Factory Method pattern provides a way to create objects without specifying the exact class of the object that will be created. It lets subclasses decide which class to instantiate.
Click to reveal answer
beginner
In the Factory Method pattern, who is responsible for creating the product objects?
The subclasses of the creator class are responsible for creating the product objects by overriding the factory method.
Click to reveal answer
intermediate
How does the Factory Method pattern help in system design?
It promotes loose coupling by separating object creation from usage, making the system easier to extend and maintain.
Click to reveal answer
intermediate
Which of the following best describes the Factory Method pattern structure?
It has a Creator class with a factory method that returns a Product interface. Subclasses override the factory method to instantiate concrete products.
Click to reveal answer
beginner
Give a real-life example of the Factory Method pattern.
Consider a document editor app that can create different types of documents (like text or spreadsheet). The app uses a factory method to create the right document type without knowing the exact class.
Click to reveal answer
What does the Factory Method pattern primarily help with?
AHandling user interface events
BManaging database connections
COptimizing memory usage
DCreating objects without specifying their exact class
✗ Incorrect
The Factory Method pattern focuses on creating objects without specifying their exact class.
Who overrides the factory method in the Factory Method pattern?
AThe base Creator class
BThe subclasses of the Creator class
CThe client code
DThe product classes
✗ Incorrect
Subclasses of the Creator class override the factory method to create specific product instances.
Which benefit does the Factory Method pattern provide?
ASeparates object creation from usage
BTight coupling between classes
CForces use of global variables
DEliminates need for interfaces
✗ Incorrect
It separates object creation from usage, promoting loose coupling.
In the Factory Method pattern, what does the factory method return?
AA concrete product instance
BA configuration file
CA product interface or abstract product
DA database connection
✗ Incorrect
The factory method returns a product interface or abstract product, allowing flexibility.
Which scenario best fits the Factory Method pattern?
ACreating different types of documents in an editor app
BSorting a list of numbers
CSending an email notification
DCalculating mathematical formulas
✗ Incorrect
Creating different document types is a classic example of the Factory Method pattern.
Explain the Factory Method pattern and how it helps in designing flexible systems.
Think about how subclasses decide which object to create.
You got /4 concepts.
Describe a real-world example where the Factory Method pattern can be applied and why.
Consider apps that handle multiple related object types.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the Factory Method pattern in system design?
easy
A. To enforce a single instance of a class throughout the application
B. To create objects directly without any abstraction
C. To define an interface for creating objects but let subclasses decide which class to instantiate
D. To provide a global point of access to a resource
Solution
Step 1: Understand the Factory Method pattern goal
The Factory Method pattern aims to separate object creation from usage by defining an interface for creating objects.
Step 2: Identify the role of subclasses
Subclasses decide which specific class to instantiate, allowing flexibility and easy extension.
Final Answer:
To define an interface for creating objects but let subclasses decide which class to instantiate -> Option C
It returns void, which conflicts with abstract method's Product return type.
Final Answer:
ConcreteCreator's factoryMethod has wrong return type -> Option A
Quick Check:
Return type mismatch causes error [OK]
Hint: Return type must match abstract method in subclass [OK]
Common Mistakes:
Ignoring return type mismatch
Thinking abstract class can't have abstract methods
Assuming static needed for Factory Method
5. You are designing a system that supports multiple types of notifications: Email, SMS, and Push. Using the Factory Method pattern, how should you organize your classes to allow easy addition of new notification types without modifying existing code?
hard
A. Create separate unrelated classes for each notification type without a common interface
B. Create an abstract NotificationCreator with a factoryMethod returning Notification; implement subclasses EmailCreator, SMSCreator, PushCreator each creating respective Notification objects
C. Use static methods in Notification class to create different notification types
D. Create a single Notification class with if-else to handle all notification types
Solution
Step 1: Apply Factory Method pattern structure
Define an abstract creator class with a factoryMethod that returns a Notification interface or abstract class.
Step 2: Implement concrete creators for each notification type
Each subclass (EmailCreator, SMSCreator, PushCreator) overrides factoryMethod to create its specific Notification object.
Step 3: Ensure easy extension without modifying existing code
Adding new notification types means adding new creator subclasses without changing existing ones, following open-closed principle.
Final Answer:
Create an abstract NotificationCreator with a factoryMethod returning Notification; implement subclasses EmailCreator, SMSCreator, PushCreator each creating respective Notification objects -> Option B
Quick Check:
Factory Method enables easy extension via subclassing [OK]
Hint: Use abstract creator + subclasses for each notification type [OK]
Common Mistakes:
Using if-else inside one class, violating open-closed principle
Using static methods, reducing flexibility
Not using a common interface for notifications
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.